Repository navigation
Thread.current returns the owning thread in fibers, with locals kept per fiber - #9963
Open
sampokuokkanen wants to merge 2 commits into
Open
sampokuokkanen wants to merge 2 commits into
sampokuokkanen wants to merge 2 commits into
Conversation
Member
|
Progress! It seems like the way we manage fibers is just different enough from CRuby that this is an ongoing challenge to migrate. This definitely gets us closer. The change still scares me, though, because there's code out there that has workarounds for the existing |
Contributor
Author
|
I looked for code that works around the old behavior:
|
Member
|
Consider this for 10.1.4.0. Land immediately after 10.1.3.0 release so it can bake. |
A few step toward getting Thread.current in general to behave like CRuby, returning the same Thread for all Fiber instances that it owns. Behind the scenes, each Fiber is still backed by a VirtualThread with an associated ThreadContext and RubyThread, but user code should generally not see those. Additional fixes will follow for internal code that has expectations about the current thread of a fiber. See other attempts to do this and issues related: * My attempt: jruby#5827 * Community attempt: jruby#1225 * An early issue: jruby#1806 Relates to test failures mentioned in jruby#9603 and workarounds from users in jruby#9600.
With Thread.current returning the owning thread inside fibers, Thread#[], []=, key? and keys read that thread's own locals, so fibers shared them. The owning thread now records which of its fibers is running, updated whenever a fiber gets control, and the fiber-local methods use that fiber's storage. This matches MRI, where they read th->ec, the running fiber's context, no matter which thread asks. Thread.current now being the owning thread also lets the two thread_variable_in_enumerator MRI tests pass, so their excludes go.
sampokuokkanen
force-pushed
the
fiber-current-thread-locals
branch
from
November 6, 2026 02:25
d42f316 to
e6bd610
Compare
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Built on #9621, with its commit included (by @headius).
#9621 makes
Thread.currentinside a fiber return the thread that owns the fiber, like CRuby. Its CI failed becauseThread#[],[]=,key?andkeysthen read the owning thread's locals, so every fiber on a thread shared them.The second commit has the owning thread remember which of its fibers is running, updated whenever a fiber gets control, and the fiber-local methods use that fiber's storage. CRuby does the same through
th->ec, which a fiber switch points at the running fiber (fiber_restore_threadin cont.c). Another thread readingt[:x]therefore sees the locals of whatever fiber is running int.This also fixes five tests in the async suite (533 → 538 of 540).
The sleep test, at least, failed because async's test framework (sus) only counts a mocked call made on the same
Thread.currentthat set up the mock.Follow-ups, not in this PR:
status,stop?andbacktracewhile a fiber runs, andraise,killandjoinonThread.currentfrom inside a fiber.As expected,
killis going to be the tricky one.