Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79a53ea

Browse files
committed
Issue #18418: After fork(), reinit all threads states, not only active ones.
Patch by A. Jesse Jiryu Davis.
2 parents b586934 + 9939cc8 commit 79a53ea

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lib/test/test_threading.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,27 @@ def background_thread(evt):
445445
self.assertEqual(out, b'')
446446
self.assertEqual(err, b'')
447447

448+
@unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
449+
def test_is_alive_after_fork(self):
450+
# Try hard to trigger #18418: is_alive() could sometimes be True on
451+
# threads that vanished after a fork.
452+
old_interval = sys.getswitchinterval()
453+
self.addCleanup(sys.setswitchinterval, old_interval)
454+
455+
# Make the bug more likely to manifest.
456+
sys.setswitchinterval(1e-6)
457+
458+
for i in range(20):
459+
t = threading.Thread(target=lambda: None)
460+
t.start()
461+
self.addCleanup(t.join)
462+
pid = os.fork()
463+
if pid == 0:
464+
os._exit(1 if t.is_alive() else 0)
465+
else:
466+
pid, status = os.waitpid(pid, 0)
467+
self.assertEqual(0, status)
468+
448469

449470
class ThreadJoinOnShutdown(BaseTestCase):
450471

Lib/threading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def _after_fork():
940940
new_active = {}
941941
current = current_thread()
942942
with _active_limbo_lock:
943-
for thread in _active.values():
943+
for thread in _enumerate():
944944
# Any lock/condition variable may be currently locked or in an
945945
# invalid state, so we reinitialize them.
946946
thread._reset_internal_locks()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ Ben Darnell
288288
Kushal Das
289289
Jonathan Dasteel
290290
Pierre-Yves David
291+
A. Jesse Jiryu Davis
291292
John DeGood
292293
Ned Deily
293294
Vincent Delft

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Core and Builtins
5151
Library
5252
-------
5353

54+
- Issue #18418: After fork(), reinit all threads states, not only active ones.
55+
Patch by A. Jesse Jiryu Davis.
56+
5457
- Issue #17974: Switch unittest from using getopt to using argparse.
5558

5659
- Issue #11798: TestSuite now drops references to own tests after execution.

0 commit comments

Comments
 (0)