Conversation
…e in DynamicEvaluationContext
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.
The problem I am trying to Solve?
DynamicEvaluationContext.apply()(the thing behind pg.oneof, pg.manyof, pg.floatv etc.) needs to remember "what decision is currently active" while the code runs. It is used to store this in two unsafe ways:A plain instance attribute (self._decision_getter) on the context object.
Thread-local storage (threading.local), which only keeps things separate per OS thread — not per asyncio.Task.
The problem: modern Python code (like agents, servers, or anything using asyncio) often runs many tasks on the same thread at the same time, taking turns. If two tasks call context.apply(...) on the same context around the same time, they'd overwrite each other's decision, causing wrong results or crashes.
Why this doesn't break anything
The old thread_local_* functions are untouched. I didn't delete or modify them — I only added new context_local_* functions next to them. Anyone using thread_local_* elsewhere is unaffected.
contextvars behaves like threading.local for normal thread-based code. If you're not using asyncio, nothing changes — each thread still gets its own isolated storage, same as before.
Regular (single-threaded, non-async) usage is 100% identical. All existing tests that check basic oneof/manyof/apply/collect behavior still pass unchanged.