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

Skip to content

Commit 9939cc8

Browse files
committed
Issue #18418: After fork(), reinit all threads states, not only active ones.
Patch by A. Jesse Jiryu Davis.
1 parent dee0434 commit 9939cc8

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
@@ -444,6 +444,27 @@ def background_thread(evt):
444444
self.assertEqual(out, b'')
445445
self.assertEqual(err, b'')
446446

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

448469
class ThreadJoinOnShutdown(BaseTestCase):
449470

Lib/threading.py

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

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Ben Darnell
279279
Kushal Das
280280
Jonathan Dasteel
281281
Pierre-Yves David
282+
A. Jesse Jiryu Davis
282283
John DeGood
283284
Ned Deily
284285
Vincent Delft

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #18418: After fork(), reinit all threads states, not only active ones.
70+
Patch by A. Jesse Jiryu Davis.
71+
6972
- Issue #16611: http.cookie now correctly parses the 'secure' and 'httponly'
7073
cookie flags.
7174

0 commit comments

Comments
 (0)