Project

General

Profile

Actions

Defect #38250

closed

config/settings.yml not closed in Setting.load_available_settings

Defect #38250: config/settings.yml not closed in Setting.load_available_settings

Added by Thomas Löber almost 4 years ago. Updated almost 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Code cleanup/refactoring
Target version:
Resolution:
Fixed
Affected version:

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 almost 4 years ago Actions #1

  • 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 almost 4 years ago Actions #2

  • 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 almost 4 years ago Actions #3

Rather than explicitly opening the file ourselves, we may also use YAML.load_file.

Updated by Thomas Löber almost 4 years ago Actions #4

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 almost 4 years ago Actions #5

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 almost 4 years ago Actions #6

  • Target version changed from 4.2.10 to 5.1.0

Updated by Go MAEDA almost 4 years ago Actions #7

  • Status changed from Resolved to Closed
Actions

Also available in: PDF Atom