Skip to content

Thread.current returns the owning thread in fibers, with locals kept per fiber - #9963

Open
sampokuokkanen wants to merge 2 commits into
jruby:masterfrom
sampokuokkanen:fiber-current-thread-locals
Open

sampokuokkanen wants to merge 2 commits into
jruby:masterfrom
sampokuokkanen:fiber-current-thread-locals

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

Built on #9621, with its commit included (by @headius).

#9621 makes Thread.current inside a fiber return the thread that owns the fiber, like CRuby. Its CI failed because Thread#[], []=, key? and keys then 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_thread in cont.c). Another thread reading t[:x] therefore sees the locals of whatever fiber is running in t.

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.current that set up the mock.

Follow-ups, not in this PR: status, stop? and backtrace while a fiber runs, and raise, kill and join on Thread.current from inside a fiber.

As expected, kill is going to be the tricky one.

@headius

headius commented Oct 30, 2026

Copy link
Copy Markdown
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 Thread.current behavior.

@sampokuokkanen

Copy link
Copy Markdown
Contributor Author

I looked for code that works around the old behavior:

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

headius commented Nov 4, 2026

Copy link
Copy Markdown
Member

Consider this for 10.1.4.0. Land immediately after 10.1.3.0 release so it can bake.

headius and others added 2 commits November 6, 2026 11:08
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

Copy link
Copy Markdown
Contributor Author

@headius
Could we merge this?
There's a fiber scheduler bug like #10028 that I'd like to fix but it depends on this working. Also if the Herb PR is merged this would be nice to have working there.

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.

2 participants