-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ContextVars are not thread-safe in the free-threaded build #121546
Comments
ContextVars are not currently thread-safe in the free-threaded build. We will want to fix that soon, but in the meantime add a suppression for the TSan reported races.
ContextVars are not currently thread-safe in the free-threaded build. We will want to fix that soon, but in the meantime add a suppression for the TSan reported races.
ContextVars are not currently thread-safe in the free-threaded build. We will want to fix that soon, but in the meantime add a suppression for the TSan reported races.
Is throwing a lock on all modifications of the cache a valid solution? Or would that be too slow? The other option is to "register" a cache in a linked list in thread state. Then have that be cleared on shutdown. I like this better. |
A lock would mean that concurrent accesses to ContextVars would not scale well. I'd be more tempted to just disable the cache in the free-threaded build and pay the increased access cost, but still scale well. Can you explain your linked list idea more? |
Flow:
Therefore, most reads (cache hits) should scale well, only cache misses and writes (sets) scale badly. |
This seems like a complicated scheme and would still not scale well for typical usage patterns. The assumption that most accesses are cache hits is probably not true for a multi-threaded program. It's a plausible assumption with the GIL because you have coarse grained accesses: one thread runs at a time and may do lots of context var accesses within that time slice. Without the GIL, that's probably not true: a context var is likely to have many threads access it in a highly interleaved (or concurrent) pattern. Contexts are per-thread. The lookup is on the current thread's context, and a context can only be active on a single thread at a time. HAMTs are persistent, in other words the contents are immutable, and mutations create a new data structure (with efficient sharing). |
Oops yea I forgot/didn't know that HAMTs are copy on write. Thanks for pointing that out. In that case I'll send a PR to disable the cache on free-threaded builds. |
…ythonGH-121740) (cherry picked from commit e904300) Co-authored-by: Ken Jin <[email protected]>
I think this was addressed by @Fidget-Spinner in #121740 |
Bug report
The ContextVar implementation is not thread-safe in the free-threaded build:
In particular, the caching is not thread-safe, although there may be other thread-safety issues as well:
cpython/Python/context.c
Lines 206 to 212 in 9c08f40
Example Test Cases:
./python -m test test_context -m test_context_threads_1
./python -m test test_decimal -m test_threading
Linked PRs
The text was updated successfully, but these errors were encountered: