Skip to content

Commit 2821f9c

Browse files
make write_env_usage atomic
1 parent 90b535d commit 2821f9c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Types.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,28 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
468468
TOML.print(io, Dict(source_file => [Dict("time" => now())]))
469469
end
470470

471-
# Append entry to log file in one chunk
471+
## Atomically write usage file
472472
usage_file = joinpath(logdir(), usage_filepath)
473-
open(usage_file, append=true) do io
474-
write(io, entry)
473+
while true
474+
# read existing usage file
475+
existing_usage = isfile(usage_file) ? read(usage_file, String) : ""
476+
477+
# Write to a temp file in the same directory as the destination
478+
temp_usage_file = tempname(logdir())
479+
open(temp_usage_file, "w") do io
480+
write(io, existing_usage)
481+
write(io, entry)
482+
end
483+
484+
# Move the temp file into place, replacing the original
485+
mv(temp_usage_file, usage_file, force = true)
486+
487+
# Check that the new file has what we want in it
488+
new_usage = read(usage_file, String)
489+
occursin(entry, new_usage) && break
490+
# If not, try again
475491
end
492+
476493
end
477494

478495
function read_package(path::String)

0 commit comments

Comments
 (0)