-
-
Notifications
You must be signed in to change notification settings - Fork 2k
with_clean_env / with_original_env is not threadsafe? #4232
Comments
It's impossible for those methods to be threadsafe. They modify global variables. Adding a mutex wouldn't help because that simply ensures that the threads have to take turns modifying global state. You'll need to handle the thread safety yourself if you call those methods. |
Oops, after reading your ticket again, I think a mutex would prevent this particular exception. My bad, sorry. However, the underlying problem would still be there, even though the exception would not be. If you're using these methods in threads, you need to make sure they have exclusive execution because they modify global state. |
@indirect since the methods take blocks, we could add our own mutex inside them, a BUNDLER_ENV_MUTEX of sorts |
ahh, gotcha. I just rewrote my script so that there is only one invocation of |
@segiddins think we should? the mutex will be held the entire time the subcommand is running, which effectively undoes threading.... |
I guess we could release the mutex by forking, setting the env back, and then releasing the lock? That sounds... much more complicated than what we have now. 😝 |
Maybe there should be a Then other ruby applications could retrieve this and pass it on for when launching new processes (e.g. with (there is |
I think a |
Agree! |
At one time, the difference was that one went up one layer of env, while the other removed all Bundler values. I'm not sure if that's still true today. A |
Introduce "Bundler.clean_env" Via #4232 Which should return the environment as it was *before* bundler modified it. Should this use "original_env" or "clean_env" or both? > At one time, the difference was that one went up one layer of env, while the other removed all Bundler values. I'm not sure if that's still true today. You can assign this ticket to me if you want.
Closed by #4303. |
I have a script that fires off many thread which each run a shell commands. Here is the code which invokes the shell commands:
On one of my runs I got this error:
The suspicious bundler code is here. Line 198 refers to
ENV.replace(ORIGINAL_ENV)
:Subsequent runs of my script never caused the problem again.
I tried writing a script to stress test this race condition and reproduce the problem but I wasn't able to.
I'm not entirely sure where in that code (or elsewhere) the problem is originating. Is it simply that any two threads in a runtime can't iterate over and add a new key to a hash at the same time? Or does the violating key addition have to happen within the actual iteration block? If the former then I think it's simply a matter of a low-probability race condition. If the latter, then I have no idea where this could be occurring.
The text was updated successfully, but these errors were encountered: