Skip to content

Keep queue waits going when a masked interrupt wakes them - #10057

Merged
headius merged 3 commits into
jruby:jruby-10.0from
sampokuokkanen:handle-interrupt-timed-queue
Nov 13, 2026
Merged

headius merged 3 commits into
jruby:jruby-10.0from
sampokuokkanen:handle-interrupt-timed-queue

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

Fixes the queue part of #10028.

Inside Thread.handle_interrupt(Object => :never), Thread#raise and Thread#kill still wake a thread that waits on a queue. Three waits gave up on that wakeup. They raised ThreadError ("interrupted in Thread::Queue#pop") inside the masked block, and then the deferred interrupt replaced it when the block ended:

  • Queue#pop(timeout:)
  • SizedQueue#push(x, timeout:)
  • SizedQueue#push(x) without a timeout

Thread#wakeup took the same path, so it also ended timed waits early with ThreadError.

CRuby keeps waiting in both cases: queue_do_pop, rb_szqueue_push.

Not fixed here:

@sampokuokkanen
sampokuokkanen force-pushed the handle-interrupt-timed-queue branch from 6708e92 to d6d94cf Compare November 12, 2026 07:13
Thread#raise and Thread#kill wake the waiting thread even inside
Thread.handle_interrupt(Object => :never). Timed Queue#pop and timed
and untimed SizedQueue#push gave up on that wakeup and raised
ThreadError inside the masked block. Thread#wakeup hit the same path,
so it cut timed waits short with ThreadError. Loop like the untimed
pop task and CRuby's queue_do_pop and rb_szqueue_push: check
interrupts, then wait again, for the rest of the timeout if there is
one.

Part of jruby#10028.
The timed pop and push tasks each had their own copy of the loop that
runs unmasked interrupts and waits again. Move it into
Queue.retryInterruptedWait. The untimed tasks keep the loop written
out, because a capturing lambda there allocates on every pop and push.
Annotate each task with the part of queue_do_pop or rb_szqueue_push it
ports.
Untimed pop and push already kept their own loops, because a
capturing lambda there allocates on every call. That left the helper
with only the two timed tasks. Write their loops out too, and explain
the retry once on BLOCKING_TAKE_TASK.
@sampokuokkanen
sampokuokkanen force-pushed the handle-interrupt-timed-queue branch from 737e421 to ecde274 Compare November 12, 2026 07:46
@sampokuokkanen
sampokuokkanen changed the base branch from master to jruby-10.0 November 12, 2026 07:46
@sampokuokkanen

Copy link
Copy Markdown
Contributor Author

I tried to refactor the loop to a helper but it would require passing a lambda which made it slow or making it complicated so keeping the loops as is.

It would be nice to have the @CRuby annotation in jruby-10.0 too.

@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.

Yup, this is the correct handling of JVM thread interrupts.

@headius headius added this to the JRuby 10.1.4.0 milestone Nov 13, 2026
@headius

headius commented Nov 13, 2026

Copy link
Copy Markdown
Member

It would be nice to have the @cruby annotation in jruby-10.0 too.

Hmmm, maybe. In a few months we'll roll a new LTS based on 10.1 and it won't matter as much. I think it might be messier than it's worth (@zev did several iterations cleaning up mismatched CRuby comments and it probably wouldn't be a trivial backport).

@headius

headius commented Nov 13, 2026

Copy link
Copy Markdown
Member

I could see a compromise where we just backport the annotation and not the extra work to clean up existing comments; at least new patches to 10.0 could use the annotation and not pollute 10.1+ when merged forward.

@sampokuokkanen I'll do that in a separate PR and fix these couple of comments then.

@headius
headius merged commit 075aa1c into jruby:jruby-10.0 Nov 13, 2026
127 checks passed
@sampokuokkanen
sampokuokkanen deleted the handle-interrupt-timed-queue branch November 15, 2026 00:39
@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