Skip to content

Keep masked sleep going and don't lose a raise followed by kill - #10064

Merged
headius merged 2 commits into
jruby:jruby-10.0from
sampokuokkanen:handle-interrupt-sleep
Nov 17, 2026
Merged

headius merged 2 commits into
jruby:jruby-10.0from
sampokuokkanen:handle-interrupt-sleep

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

Fixes #10028 (the rest of it).

  • A masked Thread#raise or Thread#kill ended Kernel#sleep early. Now sleep keeps going until its time is up or Thread#wakeup is called, like MRI's sleep_hrtime and sleep_forever.
  • After t.raise "boom"; t.kill, a thread blocked in Queue#pop or sleep died from the kill and the raise was lost. executeTask no longer checks for interrupts a second time when one was already raised, like MRI's rb_nogvl.

executeTask checked for interrupts again in its finally block, even
when the task was already throwing a raised interrupt. If Thread#raise
was followed by Thread#kill, the second check threw the kill and the
raised exception was lost. This happened in Queue#pop, with and without
a timeout.

Check again only when the task returned or was interrupted by Java,
as MRI checks interrupts after a blocking region only on normal return.
Thread#raise and Thread#kill wake a sleeping thread even when
handle_interrupt masks them. Kernel#sleep then returned early, so a
masked sleep 1 could end after 0.3 seconds.

Like MRI's sleep_hrtime and sleep_forever, run any interrupt that is
not masked and go back to sleep. Only the deadline or Thread#wakeup
ends the sleep.

Part of jruby#10028.
* @param context the current context
* @param nanoseconds time to sleep. Zero sleeps until woken.
*/
public void sleepUntilWakeup(ThreadContext context, long nanoseconds) throws InterruptedException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit-pick: not sure whether to expose this on a core clas - usually it's best to hide and be mindful whether we want things as public API, in this case:

Suggested change
public void sleepUntilWakeup(ThreadContext context, long nanoseconds) throws InterruptedException {
void sleepUntilWakeup(ThreadContext context, long nanoseconds) throws InterruptedException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Don't expose until it's obviously needed as a public API.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* @param context the current context
* @param nanoseconds time to sleep. Zero sleeps until woken.
*/
public void sleepUntilWakeup(ThreadContext context, long nanoseconds) throws InterruptedException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Don't expose until it's obviously needed as a public API.

@headius headius added this to the JRuby 10.1.4.0 milestone Nov 17, 2026
@headius
headius merged commit eedd035 into jruby:jruby-10.0 Nov 17, 2026
127 checks passed
@kares kares modified the milestones: JRuby 10.1.4.0, JRuby 10.0.9.0 Nov 18, 2026
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

Successfully merging this pull request may close these issues.

3 participants