bpo-42350: Fix Thread._reset_internal_locks() (GH-23268)

Fix the threading.Thread class at fork: do nothing if the thread is
already stopped (ex: fork called at Python exit). Previously, an
error was logged in the child process.
This commit is contained in:
Victor Stinner 2020-11-16 15:20:34 +01:00 committed by GitHub
parent 3df5c68487
commit 5909a494cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View file

@ -844,8 +844,12 @@ class Thread:
# they may be in an invalid state leading to a deadlock or crash.
self._started._at_fork_reinit()
if is_alive:
self._tstate_lock._at_fork_reinit()
self._tstate_lock.acquire()
# bpo-42350: If the fork happens when the thread is already stopped
# (ex: after threading._shutdown() has been called), _tstate_lock
# is None. Do nothing in this case.
if self._tstate_lock is not None:
self._tstate_lock._at_fork_reinit()
self._tstate_lock.acquire()
else:
# The thread isn't alive after fork: it doesn't have a tstate
# anymore.