Carry the runtime Context into thread pool workers - #4
Open
tamohannes wants to merge 1 commit into
Open
Conversation
`Runtime.ctx` resolves through the `_RT_CTX` ContextVar. A pool worker starts with an empty contextvars context, so any branch dispatched through `pool.submit(...)` or `loop.run_in_executor(...)` raised `LookupError: <ContextVar name='nu_rt_ctx'>` the moment it touched the Context. Every dispatch site now submits through a fresh `contextvars.copy_context()` taken on the calling side: `eval_parallel`, `_drive_async`, `merge`, `amerge`, `in_thread` and `a_in_thread`. The Context stays resolvable on the worker, and a copy per branch keeps a `.set()` inside a branch local to it, which is the same copy-on-write rule an asyncio.Task already gets. This turns 5 failing tests on main green (test_operators, test_strategy, test_eval_modes_e2e) and adds one regression test per dispatch site, covering `merge`, `amerge`, `in_thread` and `a_in_thread`, which had no test for this. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
make testis red on main: 5 failures, all the same error.Cause
Runtime.ctxresolves through the_RT_CTXContextVar, which the comment above it describes as per asyncio task with copy on write inheritance. That holds for tasks. It does not hold for threads: aThreadPoolExecutorworker starts with an empty contextvars context, so_RT_CTXis simply unset there.Every hand-off to the Budget's pool goes over that boundary bare:
So a parallel branch dies as soon as it touches the Context, which is what
ref._writedoes on the way intoctx.attrs.Regression from 322f9f3 ("Make Runtime.ctx per-asyncio-task via ContextVar"), 2026-07-21.
Fix
Take a fresh
contextvars.copy_context()on the calling side and submit through itsrun. Six dispatch sites:eval_parallel,_drive_async,merge,amerge,in_thread,a_in_thread.A copy rather than the caller's own context is deliberate, and it gives exactly the semantics the module comment already claims: the Context object is shared so writes to
ctx.attrsstill land where the caller can see them, while rebinding_RT_CTXinside a branch stays local to that branch. It also avoids the "cannot enter context: already entered" case, since two workers never share one Context object.Verification
Before:
5 failed, 2053 passed. After:2058 passed, 27 skipped.Added one regression test per dispatch site under a new "context across the thread boundary" section in
tests/nu/lang/runtime/test_runtime.py.merge,amerge,in_threadanda_in_threadhad no coverage for this at all. All six fail on main and pass here.Found while checking #2 on a clean clone, so this is what
make testdoes out of the box, not a code read.🤖 Generated with Claude Code