Skip to content

Thread safety of PyCapsule #145563

@eendebakpt

Description

@eendebakpt

Bug report

Bug description:

The PyCapsule object is not thread-safe. For example the read and write operations to the underlying pointer do not use atomics.

return capsule->pointer;

This can be fixed with atomic read and write operations. (same for the name field and some others)

Another potential issue is that some operations on a capsule are not atomic. For example a thread can attempt to retrieve a pointer using PyCapsule_GetPointer while a concurrent thread is updating the name and pointer using PyCapsule_SetName and PyCapsule_SetPointer. That could be addressed with locks, but I suspect that is too heavy for the capsule object.

I am leaning towards adding atomics for the read and write operations and documenting that in a PyCapsule does not guarantee its methods are atomic.

@colesbury @seberg

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions