Repository navigation
Read thread-locals safely when fibers migrate OS threads - #66
Merged
vadimskipin merged 2 commits intoJun 10, 2026
Merged
Conversation
A fiber can suspend on one scheduler thread and resume on another, but the compiler caches the thread pointer in a callee-saved register that `jump_fcontext` preserves across the switch. After a migration, thread-local reads then address the previous thread's storage and return a stale value or null. This crashed `getCurrentFiber` (null deref of `Fiber::isProxyFiber` at `0x9`) and made `getCurrentProcessor` pick the wrong io_uring ring. It worsens with inlining/unity/LTO; GCC and Clang have no `/GT` equivalent (PR 26461). Make the accessors independent of the thread pointer being cached: - `getCurrentProcessor` reads it via volatile asm (`movq %fs:0` / `mrs tpidr_el0`) so it cannot be hoisted across a suspension. - `getCurrentFiber`/`getCurrentFiberId` are `noinline`, so their reads run in a fresh activation on the current thread. Add `docs/tls.md` on using thread-local storage from fiber code.
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.
A fiber can suspend on one scheduler thread and resume on another, but the compiler caches the thread pointer in a callee-saved register that
jump_fcontextpreserves across the switch. After a migration, thread-local reads then address the previous thread's storage and return a stale value or null. This crashedgetCurrentFiber(null deref ofFiber::isProxyFiberat0x9) and madegetCurrentProcessorpick the wrong io_uring ring. It worsens with inlining/unity/LTO; GCC and Clang have no/GTequivalent (PR 26461).Make the accessors independent of the thread pointer being cached:
getCurrentProcessorreads it via volatile asm (movq %fs:0/mrs tpidr_el0) so it cannot be hoisted across a suspension.getCurrentFiber/getCurrentFiberIdarenoinline, so their reads run in a fresh activation on the current thread.Add
docs/tls.mdon using thread-local storage from fiber code.