Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-132969 Fix exception/hang when shutdown(wait=False) and a task exi…
…ted abnormally

When shutdown is called with wait=False, the executor thread keeps running
even after the ProcessPoolExecutor's state is reset. The executor then tries
to replenish the worker processes pool resulting in an error and a potential hang
when it comes across a worker that has died. Fixed the issue by having
_adjust_process_count() return without doing anything if the ProcessPoolExecutor's
state has been reset.

Added unit tests to validate two scenarios:
max_workers < num_tasks (exception)
max_workers > num_tasks (exception + hang)
  • Loading branch information
ogbiggles committed Apr 30, 2025
commit 3edad4cb415b9508a6feab58f3c5962e494bb577
4 changes: 4 additions & 0 deletions Lib/concurrent/futures/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ def _start_executor_manager_thread(self):
self._executor_manager_thread_wakeup

def _adjust_process_count(self):
# gh-132969
if self._processes is None:
return

# if there's an idle process, we don't need to spawn a new one.
if self._idle_worker_semaphore.acquire(blocking=False):
return
Expand Down
47 changes: 47 additions & 0 deletions Lib/test/test_concurrent_futures/test_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ def sleep_and_print(t, msg):
sys.stdout.flush()


def failing_task_132969(n: int) -> int:
raise ValueError("failing task")


def good_task_132969(n: int) -> int:
time.sleep(0.1 * n)
return n



class ExecutorShutdownTest:
def test_run_after_shutdown(self):
self.executor.shutdown()
Expand Down Expand Up @@ -330,6 +340,43 @@ def test_shutdown_no_wait(self):
# shutdown.
assert all([r == abs(v) for r, v in zip(res, range(-5, 5))])

def _run_test_issue_132969(self, max_workers: int) -> int:
# max_workers=2 will repro exception
# max_workers=4 will repro exception and then hang

import multiprocessing as mp

# Repro conditions
# max_tasks_per_child=1
# a task ends abnormally
# shutdown(wait=False) is called
executor = futures.ProcessPoolExecutor(
max_workers=max_workers,
max_tasks_per_child=1,
mp_context=mp.get_context("forkserver"))
f1 = executor.submit(good_task_132969, 1)
f2 = executor.submit(failing_task_132969, 2)
f3 = executor.submit(good_task_132969, 3)
result:int = 0
try:
result += f1.result()
result += f2.result()
result += f3.result()
except ValueError:
# stop processing results upon first exception
pass

executor.shutdown(wait=False)
return result

def test_shutdown_len_exception_132969(self):
result = self._run_test_issue_132969(2)
self.assertEqual(result, 1)

def test_shutdown_process_hang_132969(self):
result = self._run_test_issue_132969(4)
self.assertEqual(result, 1)


create_executor_tests(globals(), ProcessPoolShutdownTest,
executor_mixins=(ProcessPoolForkMixin,
Expand Down