Skip to content

Commit d8ff44c

Browse files
authored
bpo-40089: Fix threading._after_fork() (GH-19191)
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.
1 parent 5a58c52 commit d8ff44c

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
@@ -1423,7 +1423,15 @@ def _after_fork():
14231423

14241424
# fork() only copied the current thread; clear references to others.
14251425
new_active = {}
1426-
current = current_thread()
1426+
1427+
try:
1428+
current = _active[get_ident()]
1429+
except KeyError:
1430+
# fork() was called in a thread which was not spawned
1431+
# by threading.Thread. For example, a thread spawned
1432+
# by thread.start_new_thread().
1433+
current = _MainThread()
1434+
14271435
_main_thread = current
14281436

14291437
# 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)