Documentation
Summary
This is a tracking issue for the ongoing effort to improve thread safety documentation in CPython, covering both Python-level guarantees for built-in types and C API-level annotations for extension authors.
Completed work
Thread safety page and built-in type guarantees (gh-142518)
C API thread safety annotations (gh-145254)
In progress (at the time of writing)
Remaining work
stdlib type documentation
After built-in types, document thread-safety guarantees for stdlib types. Suggested priority:
Rationale: deque first because it's the most likely to be shared across threads today. io types next because concurrent file access is common and the current docs are inadequate. The collections types after that, ordered by usage frequency and concurrency risk. array.array last - niche usage, but mutable so still worth documenting.
Free-threading programming guide (new HOWTO)
A new Doc/howto/free-threading-guide.rst, alongside the existing two free-threading HOWTOs. The existing free-threading-python.rst stays focused on "what is free-threading / what changed." The new guide focuses on how to write correct concurrent code.
Topics to cover:
Cross-reference from free-threading-python.rst and from the per-type thread safety page.
Other documentation gaps
C API annotations
Infrastructure improvements
Documentation
Summary
This is a tracking issue for the ongoing effort to improve thread safety documentation in CPython, covering both Python-level guarantees for built-in types and C API-level annotations for extension authors.
Completed work
Thread safety page and built-in type guarantees (gh-142518)
C API thread safety annotations (gh-145254)
incompatible,compatible,distinct,shared,atomicthreadsafety.rstIn progress (at the time of writing)
Remaining work
stdlib type documentation
After built-in types, document thread-safety guarantees for stdlib types. Suggested priority:
collections.deque- widely used as a thread-safe queue substitute; people already assume it's safe; heavily used in asyncio internalsiotypes (BufferedReader,BufferedWriter,TextIOWrapper) - file I/O is inherently concurrent in real programs; existing thread-safety notes are scattered and incompletecollections.defaultdict- the__missing__call introduces non-obvious concurrency questions (factory call is not atomic with insertion)collections.OrderedDict- has its own internal locking that differs from plain dictcollections.Counter- commonly used for aggregation across threadsarray.array- mutable typed array, relevant for numeric/scientific code going parallelRationale:
dequefirst because it's the most likely to be shared across threads today.iotypes next because concurrent file access is common and the current docs are inadequate. Thecollectionstypes after that, ordered by usage frequency and concurrency risk.array.arraylast - niche usage, but mutable so still worth documenting.Free-threading programming guide (new HOWTO)
A new
Doc/howto/free-threading-guide.rst, alongside the existing two free-threading HOWTOs. The existingfree-threading-python.rststays focused on "what is free-threading / what changed." The new guide focuses on how to write correct concurrent code.Topics to cover:
threading.Lockand other synchronization primitivesconcurrent.futures, thread-local storageCross-reference from
free-threading-python.rstand from the per-type thread safety page.Other documentation gaps
queuemodule docs w.r.t. free-threaded buildsC API annotations
PyObject_*APIs (creation, attribute access, comparison, etc.)PySequence_*PyMapping_*PyNumber_*PyIter_*Py_INCREF,Py_DECREF,Py_NewRef, etc.)PyList_*PyDict_*PySet_*PyTuple_*PyUnicode_*PyObject_GetBuffer,PyBuffer_Release, etc.)Infrastructure improvements
threadsafety.datentries match actual function signaturescollections