Skip to content

Add thread safety annotations for C API functions #145254

Description

@lysnikolaou

Documentation

CPython's C API documentation has no systematic way to communicate the thread safety guarantees of individual APIs. As the free-threaded build matures, this information is increasingly important for extension authors.

This is analogous to how return value ownership is documented today, with automatically-inserted "Return value: New reference." / "Return value: Borrowed reference." annotations. We should do the same for thread safety.

Proposed levels

Three levels, ordered from least to most safe:

  • Thread-incompatible: Not safe even with external locking
  • Thread-compatible: Safe if the caller serializes all access with external locks
  • Safe: Safe to call from multiple threads on shared objects

Each annotation links to the corresponding glossary term. The thread-safe term already exists in the glossary, thread-compatible and thread-incompatible would be added.

I have PRs ready for both CPython and python-docs-theme. Feedback welcome, especially on the choice and naming of levels.

Linked PRs

Activity

  1. added a commit that references this issue on Feb 26, 2026
  2. lysnikolaou commented on Mar 6, 2026

    @lysnikolaou
    MemberAuthor

    I've decided to increase the number of levels to the following:

    • incompatible: Not safe even with external locking
    • compatible: Safe if the caller serializes all access with external locks
    • distinct: Safe to call without external synchronization on distinct objects
    • shared: Safe to call without external synchronization on the same shared object
    • atomic: Appears atomic to all threads
  3. added a commit that references this issue on Mar 12, 2026
  4. added a commit that references this issue on Mar 12, 2026
  5. added a commit that references this issue on Mar 12, 2026
  6. added 3 commits that reference this issue on Mar 18, 2026
  7. added a commit that references this issue on Mar 19, 2026
  8. added a commit that references this issue on Mar 19, 2026
  9. added 2 commits that reference this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions