Skip to content

Pythread_ident_t is not used consistently and could be more robust. #123983

@culler

Description

@culler

Bug report

Bug description:

Currently, pycore_pythread.h contains:

typedef unsigned long long PyThread_ident_t;

and pycore_lock.h contains:

typedef struct {
     PyMutex mutex;
     unsigned long long thread;  // i.e., PyThread_get_thread_ident_ex()
     size_t level;
 } _PyRecursiveMutex;

This is clearly wrong. If a typedef exists then it should be used.

But the type itself has drawbacks which make it less robust than it could be. While
the problem is not internal to python, and results from misuse, nonetheless python could guard against the misuse.

The context in which I encountered a problem was the cypari project, which provides a Cython wrapper for the PARI number theory library. The misuse has two parts:
(1) Cython includes internal Python headers (pycore_lock.h)
(2) The PARI library deals with the issue that 64 bit Windows does not have 64 bit
longs by using the following (amazing) hack:
#define long long long
Including pycore_lock.h after parigen.h, as Cython does, causes a compiler error.

A more robust type, not vulnerable to these sorts of shenanigans, for PyThread_ident_t would be size_t. A modest amount of casting is required to
implement this, since _Py_atomic_load_ullong_relaxed has to be replaced by
_Py_atomic_load_ssize_relaxed.

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions