Skip to content

Commit 8b584e4

Browse files
authored
fix: errors when writing to backup file from different processes (#256)
1 parent bf943d4 commit 8b584e4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/unleash/backup_file_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.save!(toggle_data)
66
Unleash.logger.debug "Will save toggles to disk now"
77

88
backup_file = Unleash.configuration.backup_file
9-
backup_file_tmp = "#{backup_file}.tmp"
9+
backup_file_tmp = "#{backup_file}.tmp-#{Process.pid}"
1010

1111
File.open(backup_file_tmp, "w") do |file|
1212
file.write(toggle_data)

spec/unleash/toggle_fetcher_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@
6565
backup_file = Unleash.configuration.backup_file
6666
expect(File.exist?(backup_file)).to eq(true)
6767
end
68+
69+
# NOTE: this is a frequent issue with Puma (in clustered mode). See Unleash/unleash-ruby-sdk#108
70+
it 'does not log errors due to concurrent forked processes' do
71+
skip 'Fork not supported on current platform' unless Process.respond_to?(:fork)
72+
73+
log_file = ->(pid) { File.join(Dir.tmpdir, "#{pid}.log") }
74+
pids = Array.new(10) do
75+
fork do
76+
Unleash.logger = Logger.new(log_file.call(Process.pid))
77+
expect(Unleash.logger).not_to receive(:error)
78+
described_class.new engine
79+
end
80+
end
81+
82+
pids.each do |pid|
83+
Process.wait(pid)
84+
process_status = $? # rubocop:disable Style/SpecialGlobalVars
85+
error_log_file = log_file.call(pid)
86+
expect(File.exist?(error_log_file))
87+
88+
error_msg = "Process #{pid} failed with errors:\n#{File.read(error_log_file).lines[1..].join}"
89+
expect(process_status).to be_success, error_msg
90+
end
91+
end
6892
end
6993
end
7094

0 commit comments

Comments
 (0)