-
-
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
Mutex sleeps indefinitely with timeout <= 0.0001 seconds #4862
Comments
This sleep is likely using the available sleeping options from the JDK, most of which have a minimum time of 1ms. Ignoring that, what do you need such small sleep times for? Or are you just trying to get a sleep time that's small enough to not be noticeable but not instantaneous? |
A simple fix for this would be to detect whether the incoming value is below some ms threshold but nonzero and not sleep at all. |
I have a fix. Perhaps there's no tests for Mutex that test zero and low-timeout sleeps? o_O |
@headius Yeah, I was just using a sleep that was nearly instantaneous to avoid any noticeable overhead but allow a pending lock request from another thread to be serviced, if one is available. A small value like 0.001 would be perfectly fine for my case. I'd just happened to pick 0 initially because it worked fine on MRI. That's great that you've found a fix already. Thanks much for looking into this! |
@camlow325 I have committed a patch that I believe does what you want it to: it always release and re-acquire the lock, but for sleep values smaller than the JDK's lock sleep granularity it will not bother to attempt to sleep. Spec added for tiny Mutex#sleep times as well. Thanks for the report! I find it incredible that nobody has reported it before. |
Environment
Provide at least:
jruby -v
) and command line (flags, JRUBY_OPTS, etc)JRuby 9.1.14.0
uname -a
)Windows
Expected Behavior
When
sleep()
is called with a timeout which is 0.0001 or less (including 0) seconds on a locked mutex, thesleep()
appears to be indefinite. For MRI 2.4.2, however, the sleep times out quickly with these smaller values.I ran the following script to reproduce this issue:
With MRI 2.4.2, the following output appears and the script ends normally:
Actual Behavior
With JRuby 9.1.14.0, only part of the output appears:
Even after leaving the script running for several minutes, the next expected message never appears: "Done sleeping for 0.0001 seconds". The same apparent hang appears if a sleep for 0 seconds - without the preceding sleep for 0.0001 seconds - occurs.
Here is a backtrace for the main thread while in the hang state:
I had some code which would sleep for 0 seconds periodically to allow a waiter to take the mutex and change some state to interrupt a long running process. Bumping the timeout up to a slightly higher value - like 0.001 seconds - is a reasonable workaround. Just thought it might be good to point out the discrepancy as compared to MRI Ruby behavior.
The text was updated successfully, but these errors were encountered: