[issue40092] Crash in _PyThreadState_DeleteExcept() at fork in the process child

STINNER Victor report at bugs.python.org
Fri Mar 27 13:16:55 EDT 2020


New submission from STINNER Victor <vstinner at python.org>:

At fork, Python calls PyOS_AfterFork_Child() in the child process which indirectly calls _PyThreadState_DeleteExcept() whichs calls release_sentinel() of the thread which releases the thread state lock (threading.Thread._tstate_lock).

Problem: using a lock after fork is unsafe and can crash.

That's exactly what happens randomly on AIX when stressing ThreadJoinOnShutdown.test_reinit_tls_after_fork() of test_threading:
https://bugs.python.org/issue40068#msg365031

There are different options to solve this issue:

* Reset _tstate_lock before using it... not sure that it's worth it, since we are going to delete the threading.Thread object with its _tstate_lock object anymore. After calling fork, the child process has exactly 1 thread: all other threads have been removed.

* Modify release_sentinel() to not use the lock: avoid PyThread_release_lock() call.

----------
components: Interpreter Core
messages: 365173
nosy: vstinner
priority: normal
severity: normal
status: open
title: Crash in _PyThreadState_DeleteExcept() at fork in the process child
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40092>
_______________________________________


More information about the Python-bugs-list mailing list