Skip to content

Data race with Context ctx_vars under free-threading #155519

Description

@nascheme

Bug report

Bug description:

The HAMT held by a context is immutable, but the pointer to it is not: contextvar_set() and contextvar_del() install a new HAMT and drop the reference to the previous one. Only the thread that entered a context can do that (a context cannot be entered by two threads at once), but any thread can read a context object at any time -- ctx.copy(), len(ctx), ctx.items(), etc. Such a reader has to acquire its own reference to the HAMT under the context's lock; reading ctx_vars unlocked lets the writer deallocate the HAMT while the reader is walking it.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions