Skip to content

Commit a514ccb

Browse files
bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19193)
If fork was not called by a thread spawned by threading.Thread, threading._after_fork() now creates a _MainThread instance for _main_thread, instead of a _DummyThread instance. (cherry picked from commit d8ff44c) Co-authored-by: Victor Stinner <[email protected]>
1 parent 717f166 commit a514ccb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Lib/threading.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,15 @@ def _after_fork():
13401340

13411341
# fork() only copied the current thread; clear references to others.
13421342
new_active = {}
1343-
current = current_thread()
1343+
1344+
try:
1345+
current = _active[get_ident()]
1346+
except KeyError:
1347+
# fork() was called in a thread which was not spawned
1348+
# by threading.Thread. For example, a thread spawned
1349+
# by thread.start_new_thread().
1350+
current = _MainThread()
1351+
13441352
_main_thread = current
13451353

13461354
# reset _shutdown() locks: threads re-register their _tstate_lock below
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix threading._after_fork(): if fork was not called by a thread spawned by
2+
threading.Thread, threading._after_fork() now creates a _MainThread instance
3+
for _main_thread, instead of a _DummyThread instance.

0 commit comments

Comments
 (0)