Skip to content

ContextVars are not thread-safe in the free-threaded build #121546

Closed
@colesbury

Description

@colesbury

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

if (var->var_cached != NULL &&
var->var_cached_tsid == ts->id &&
var->var_cached_tsver == ts->context_ver)
{
*val = var->var_cached;
goto found;
}

Example Test Cases:

  • ./python -m test test_context -m test_context_threads_1
  • ./python -m test test_decimal -m test_threading

Linked PRs

Metadata

Metadata

Labels

3.13bugs and security fixes3.14bugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions