nascheme · GitHub

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

Read the original on github.com ↗