Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPython should not assume that pthread_self returns the same value in fork parent and child #126688

Closed
nico opened this issue Nov 11, 2024 · 2 comments
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@nico
Copy link

nico commented Nov 11, 2024

Summarized from SerenityOS/serenity#25263 by request of @colesbury:

It seems that at least Python 3.13 relies on pthread_self returning the same value in both the parent and the child process.
PyThread_get_thread_ident_ex

POSIX doesn't seem to mention if the value returned by pthread_self should be inherited by the child process.
pthread_self
fork

(It is the case on Linux, Hurd, FreeBSD, OpenBSD, Cygwin. It isn't on SerenityOS, and also not on Solaris 9/HP-UX 11 per https://bugs.python.org/issue7242.

It looks like the code add broke things is from this commit e21057b added in #118523 as part of #117657

Linked PRs

@colesbury colesbury self-assigned this Nov 11, 2024
@colesbury colesbury added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 11, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Nov 11, 2024
The PyMutex implementation supports unlocking after fork because we
clear the list of watiers in parking_lot.c. This doesn't work as well
for _PyRecursiveMutex because on some systems, such as SerenityOS, the
thread id is not preserved across fork().
colesbury added a commit to colesbury/cpython that referenced this issue Nov 11, 2024
The PyMutex implementation supports unlocking after fork because we
clear the list of watiers in parking_lot.c. This doesn't work as well
for _PyRecursiveMutex because on some systems, such as SerenityOS, the
thread id is not preserved across fork().
colesbury added a commit to colesbury/cpython that referenced this issue Nov 11, 2024
colesbury added a commit that referenced this issue Nov 12, 2024
The PyMutex implementation supports unlocking after fork because we
clear the list of waiters in parking_lot.c. This doesn't work as well
for _PyRecursiveMutex because on some systems, such as SerenityOS, the
thread id is not preserved across fork().
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 12, 2024
The PyMutex implementation supports unlocking after fork because we
clear the list of waiters in parking_lot.c. This doesn't work as well
for _PyRecursiveMutex because on some systems, such as SerenityOS, the
thread id is not preserved across fork().
(cherry picked from commit 5610860)

Co-authored-by: Sam Gross <colesbury@gmail.com>
colesbury added a commit that referenced this issue Nov 12, 2024
The PyMutex implementation supports unlocking after fork because we
clear the list of waiters in parking_lot.c. This doesn't work as well
for _PyRecursiveMutex because on some systems, such as SerenityOS, the
thread id is not preserved across fork().
(cherry picked from commit 5610860)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@oskar-skog
Copy link

I think this can be closed now.

@ericsnowcurrently
Copy link
Member

There may be other code that makes the same assumption about the thread ID. For example, in PyOS_AfterFork_Child():

assert(tstate->thread_id == PyThread_get_thread_ident());
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

This would break only on debug builds (or at least !defined(NDEBUG)).

I'm guessing there are other places too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants