Skip to content

Commit 27ff2c8

Browse files
gpsheaddaniel-shieldsblurb-it[bot]
authored
[3.14] gh-101267: ProcessPoolExecutor no longer shares 1 BrokenProcessPool exception among all failed futures (GH-101268) (#151431)
* gh-101267: ProcessPoolExecutor no longer shares 1 BrokenProcessPool exception among all failed futures (GH-101268) (cherry picked from commit 3c00ebc) Co-authored-by: Daniel Shields <daniel.shields@twosigma.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Gregory P. Smith <greg@krypto.org> (cherry picked from commit 4e8c9c6) * Drop the abrupt-exit-code reporting from the 3.14 backport Reporting the exit codes of processes that died without a known cause is a new feature, not part of the gh-101267 bugfix. Keep only the bugfix on 3.14: each failed future gets its own BrokenProcessPool exception instead of one shared instance. --------- Co-authored-by: Daniel Shields <daniel.shields@twosigma.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 7186413 commit 27ff2c8

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

Lib/concurrent/futures/process.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,20 @@ def _terminate_broken(self, cause):
469469
executor._shutdown_thread = True
470470
executor = None
471471

472-
# All pending tasks are to be marked failed with the following
473-
# BrokenProcessPool error
474-
bpe = BrokenProcessPool("A process in the process pool was "
475-
"terminated abruptly while the future was "
476-
"running or pending.")
472+
# All pending tasks are to be marked failed with a
473+
# BrokenProcessPool error, as separate instances to avoid sharing
474+
# a traceback (gh-101267).
475+
cause_tb = None
477476
if cause is not None:
478-
bpe.__cause__ = _RemoteTraceback(
479-
f"\n'''\n{''.join(cause)}'''")
477+
cause_tb = f"\n'''\n{''.join(cause)}'''"
480478

481479
# Mark pending tasks as failed.
482480
for work_id, work_item in self.pending_work_items.items():
481+
bpe = BrokenProcessPool("A process in the process pool was "
482+
"terminated abruptly while the future was "
483+
"running or pending.")
484+
if cause_tb is not None:
485+
bpe.__cause__ = _RemoteTraceback(cause_tb)
483486
try:
484487
work_item.future.set_exception(bpe)
485488
except _base.InvalidStateError:

Lib/test/test_concurrent_futures/test_process_pool.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import threading
55
import time
6+
import traceback
67
import unittest
78
import unittest.mock
89
from concurrent import futures
@@ -61,6 +62,31 @@ def test_killed_child(self):
6162
# Submitting other jobs fails as well.
6263
self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8)
6364

65+
def test_broken_process_pool_traceback(self):
66+
# When a child process is abruptly terminated, the whole pool gets
67+
# "broken", and a BrokenProcessPool exception should be created
68+
# for each future instead of sharing one exception among all futures.
69+
event = self.create_event()
70+
futures = [self.executor.submit(event.wait) for _ in range(3)]
71+
p = next(iter(self.executor._processes.values()))
72+
p.terminate()
73+
for fut in futures:
74+
# Don't use assertRaises(): it clears the traceback off exc.
75+
try:
76+
fut.result()
77+
except BrokenProcessPool as exc:
78+
tb = exc.__traceback__
79+
else:
80+
self.fail("BrokenProcessPool not raised")
81+
count = sum(
82+
1
83+
for frame_summary in traceback.extract_tb(tb)
84+
if frame_summary.filename == __file__
85+
)
86+
# This code file should appear exactly once in the traceback.
87+
# A shared exception would accumulate a frame per result() call.
88+
self.assertEqual(count, 1)
89+
6490
def test_map_chunksize(self):
6591
def bad_map():
6692
list(self.executor.map(pow, range(40), range(40), chunksize=-1))

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ Charlie Shepherd
17351735
Bruce Sherwood
17361736
Gregory Shevchenko
17371737
Hai Shi
1738+
Daniel Shields
17381739
Alexander Shigin
17391740
Pete Shinners
17401741
Michael Shiplett
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
When a worker process terminates unexpectedly,
2+
:class:`concurrent.futures.ProcessPoolExecutor` now sets a separate
3+
:exc:`~concurrent.futures.process.BrokenProcessPool` exception on each pending
4+
future instead of sharing a single instance among them all. Sharing one
5+
exception produced malformed tracebacks: each
6+
:meth:`Future.result() <concurrent.futures.Future.result>` call re-raised the
7+
same object, appending another copy of the traceback to it.

0 commit comments

Comments
 (0)