Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite force_symlink to avoid a race condition #833

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ gaproot() = @get_scratch!(scratch_key)
#
#############################################################################

function force_symlink(p::AbstractString, np::AbstractString)
rm(np; force = true)
symlink(p, np)
# ensure `source` is a symlink pointing to `target` in a way that is hopefully
# safe against races with other Julia processes doing the exact same thing
function force_symlink(source::AbstractString, target::AbstractString)
# We previously used `rm` followed by `symlink`, but this can cause a
# race if multiple processes invoke `rm` concurrently (which works if
# one uses `force=true`), and then try to invoke `symlink`
# concurrently (which then fails in all but one process).
#
# So instead we create the symlink with a temporary name, and then use
# an atomic `rename` to rename it to the `target` name. The latter
# unfortunately requires invoking an undocumented function.
tmpfile = tempname(dirname(abspath(target)); cleanup=false)
symlink(source, tmpfile)
Base.Filesystem.rename(tmpfile, target)
return nothing
end

function read_sysinfo_gap(dir::String)
Expand Down