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

with_scratch_directory is not concurrent/thread safe and not nestable #15

Open
tkf opened this issue Sep 13, 2020 · 1 comment
Open

Comments

@tkf
Copy link
Contributor

tkf commented Sep 13, 2020

This implementation of with_scratch_directory

function with_scratch_directory(f::Function, scratch_dir::String)
try
SCRATCH_DIR_OVERRIDE[] = scratch_dir
f()
finally
SCRATCH_DIR_OVERRIDE[] = nothing
end
end

is

  1. not concurrent safe (mixing it with @async can cause bugs),
  2. not thread safe, and
  3. not nestable.

The last point is about

julia> with_scratch_directory("a") do
           @show Scratch.SCRATCH_DIR_OVERRIDE[]
           with_scratch_directory("b") do
               @show Scratch.SCRATCH_DIR_OVERRIDE[]
           end
           @show Scratch.SCRATCH_DIR_OVERRIDE[]
       end
Scratch.SCRATCH_DIR_OVERRIDE[] = "a"
Scratch.SCRATCH_DIR_OVERRIDE[] = "b"
Scratch.SCRATCH_DIR_OVERRIDE[] = nothing

where the last line should be Scratch.SCRATCH_DIR_OVERRIDE[] = "a".

I think with_scratch_directory is a perfect example where using a context variable JuliaLang/julia#35833 is useful.

If you are curious, here is a branch of Scratch.jl that uses ContextVariablesX.jl; a proof-of-concept/backport of ContextVariables I proposed in the PR JuliaLang/julia#35833. We just need to change a few lines:
v1.0.1...tkf:contextvariablesx

@staticfloat
Copy link
Member

Well we can't fix the concurrency issues without doing some kind of TLS, but we can fix 3 rather easily by saving the old value then restoring it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants