-
-
Notifications
You must be signed in to change notification settings - Fork 925
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
[Truffle] Threads might not exit sleep state when interrupted #3131
Comments
I think |
@pitr-ch But I'm quite clearly seeing it being woken up. The |
There is a tension between the script shown and this spec: To show the problem in the snippet, you could call The reason the code above does not deadlock on MRI is just luck because the threads in the pool get in sleep before the main thread continues. A fix to make sure they are sleeping would be: I am not sure what is better. |
I implemented the MRI semantics, so Thread#wakeup/run only wakes up the thread if it is actually sleeping. |
It's currently possible for a Ruby thread to ignore being awoken. It looks like a race condition of sorts, so you might need to run the following example a few times to see the issue:
Some debugging notes:
When a Ruby thread goes to sleep (
Kernel#sleep
), its sleep status is set toStatus.SLEEP
[1]. WhenThread.run
is called, the status is changed toStatus.RUN
and the Java thread backing the Ruby thread is interrupted [2]. The exception handler checks the status and sets a flag to determine whether to break out of the sleep [3].Based on some logging I added, the exception handler is seeing the thread's status as
Staus.SLEEP
, notStatus.RUN
and as such, it's not setting theshouldWakeUp
flag. When the exception is propagated, it's handled inThreadManager
and stops propagating up [4]. At this point,result
isnull
and the action corresponding to the thread sleep is executed again [5]. This is the normal flow of events (i.e., the block is supposed to be executed again), but since theshouldWakeUp
flag wasn't properly set, the thread goes to sleep again and interrupt is now lost.[1] --
jruby/truffle/src/main/java/org/jruby/truffle/runtime/subsystems/ThreadManager.java
Line 122 in b91c9ed
[2] --
jruby/truffle/src/main/java/org/jruby/truffle/runtime/core/RubyThread.java
Lines 173 to 176 in 5c8c448
[3] --
jruby/truffle/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Lines 1829 to 1831 in b91c9ed
[4] -- https://github.com/jruby/jruby/blob/master/truffle/src/main/java/org/jruby/truffle/runtime/subsystems/ThreadManager.java#L134
[5] -- https://github.com/jruby/jruby/blob/master/truffle/src/main/java/org/jruby/truffle/runtime/subsystems/ThreadManager.java#L103-L105
The text was updated successfully, but these errors were encountered: