Skip to content

Commit 0ece422

Browse files
committed
PR feedback
1 parent e9c07b5 commit 0ece422

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

Lib/concurrent/futures/process.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,20 @@ def _terminate_broken(self, cause):
474474
bpe = BrokenProcessPool("A process in the process pool was "
475475
"terminated abruptly while the future was "
476476
"running or pending.")
477+
cause_str = None
477478
if cause is not None:
478-
bpe.__cause__ = _RemoteTraceback(
479-
f"\n'''\n{''.join(cause)}'''")
479+
cause_str = ''.join(cause)
480480
else:
481-
# No cause known, so try to report some helpful info about
482-
# which process(es) terminated and with what exit code
481+
# No cause known, synthesize from child process exitcodes
483482
errors = []
484483
for p in self.processes.values():
485-
if p.exitcode: # Report any nonzero exit codes
486-
errors.append(f"Process {p.pid} terminated abruptly with exit code {p.exitcode}")
484+
if p.exitcode is not None and p.exitcode != 0:
485+
errors.append(f"Process {p.pid} terminated abruptly "
486+
f"with exit code {p.exitcode}")
487487
if errors:
488-
bpe.__cause__ = _RemoteTraceback("\n".join(errors))
488+
cause_str = "\n".join(errors)
489+
if cause_str:
490+
bpe.__cause__ = _RemoteTraceback(f"\n'''\n{cause_str}'''")
489491

490492
# Mark pending tasks as failed.
491493
for work_id, work_item in self.pending_work_items.items():

Lib/test/test_concurrent_futures/test_process_pool.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ctypes
21
import os
32
import queue
43
import sys
@@ -108,21 +107,25 @@ def test_traceback(self):
108107
f1.getvalue())
109108

110109
@staticmethod
111-
def _segfault():
112-
ctypes.string_at(0)
110+
def _terminate_abruptly_with_exit_code(exit_code):
111+
os._exit(exit_code)
113112

114-
def test_traceback_when_child_process_segfaults(self):
113+
def test_traceback_when_child_process_terminates_abruptly(self):
115114
# gh-139462 enhancement - BrokenProcessPool exceptions
116115
# should describe which process terminated.
117-
future = self.executor.submit(self._segfault)
118-
with self.assertRaises(Exception) as cm:
116+
exit_code = 99
117+
future = self.executor.submit(
118+
self._terminate_abruptly_with_exit_code,
119+
exit_code
120+
)
121+
with self.assertRaises(BrokenProcessPool) as bpe:
119122
future.result()
120123

121-
bpe = cm.exception
122-
self.assertIs(type(bpe), BrokenProcessPool)
123-
cause = bpe.__cause__
124-
self.assertIs(type(cause), futures.process._RemoteTraceback)
125-
self.assertIn("terminated abruptly with exit code", cause.tb)
124+
cause = bpe.exception.__cause__
125+
self.assertIsInstance(cause, futures.process._RemoteTraceback)
126+
self.assertIn(
127+
f"terminated abruptly with exit code {exit_code}", cause.tb
128+
)
126129

127130
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
128131
@hashlib_helper.requires_hashdigest('md5')

0 commit comments

Comments
 (0)