Defect #38250
closedconfig/settings.yml not closed in Setting.load_available_settings
0%
Description
The file config/settings.yml is not closed after it is read in Setting.load_available_settings.
This can cause problems during development, for example when changing Git branches while the Rails server is running (error: unable to unlink old 'config/settings.yml').
The solution is to pass a block to File.open, as it will automatically close the file after the block is executed.
def self.load_available_settings
  File.open("#{Rails.root}/config/settings.yml") do |f|
    YAML.load(f).each do |name, options|
      define_setting name, options
    end
  end
end
  
       Updated by Go MAEDA over 2 years ago
      Updated by Go MAEDA over 2 years ago
      
    
    - Subject changed from settings.yml not closed to config/settings.yml not closed in Setting.load_available_settings
- Status changed from New to Confirmed
- Target version set to 4.2.10
       Updated by Go MAEDA over 2 years ago
      Updated by Go MAEDA over 2 years ago
      
    
    - Status changed from Confirmed to Resolved
- Assignee set to Go MAEDA
- Resolution set to Fixed
Committed the fix. Thank you for reporting and fixing the issue.
       Updated by Holger Just over 2 years ago
      Updated by Holger Just over 2 years ago
      
    
    Rather than explicitly opening the file ourselves, we may also use YAML.load_file.
       Updated by Thomas Löber over 2 years ago
      Updated by Thomas Löber over 2 years ago
      
    
    Until today, I didn't know that this method even existed. You never stop learning :-)
Here is a new patch using YAML.load_file:
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -320,7 +320,7 @@
   end
   def self.load_available_settings
-    YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options|
+    YAML.load_file(Rails.root + "config/settings.yml").each do |name, options|
       define_setting name, options
     end
   end
       Updated by Go MAEDA over 2 years ago
      Updated by Go MAEDA over 2 years ago
      
    
    Holger Just wrote:
Rather than explicitly opening the file ourselves, we may also use
YAML.load_file.
Thank you, updated the code in r22101.
       Updated by Go MAEDA over 2 years ago
      Updated by Go MAEDA over 2 years ago
      
    
    - Target version changed from 4.2.10 to 5.1.0