Skip to content

Commit e7ed0e8

Browse files
committed
Atomic LastRun.write
ResultMerger already writes .resultset.json via tempfile + rename (atomic_write_resultset), but the analogous LastRun.write opens .last_run.json with `w+` and dumps directly — a concurrent reader can hit a truncated or empty file. Apply the same tempfile + rename pattern. Both files matter to parallel-tests workers running MaximumCoverageDrop checks.
1 parent e229b96 commit e7ed0e8

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

lib/simplecov/last_run.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ def read
2020
JSON.parse(json, symbolize_names: true)
2121
end
2222

23+
# Write to a process-private temp file, then atomically rename, so a
24+
# concurrent reader (e.g. another parallel-tests worker checking
25+
# MaximumCoverageDrop) never sees a half-written file.
2326
def write(json)
24-
File.open(last_run_path, "w+") do |f|
25-
f.puts JSON.pretty_generate(json)
26-
end
27+
temp_path = "#{last_run_path}.#{Process.pid}.tmp"
28+
File.open(temp_path, "w") { |f| f.puts JSON.pretty_generate(json) }
29+
File.rename(temp_path, last_run_path)
2730
end
2831
end
2932
end

0 commit comments

Comments
 (0)