You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiprocessing.Pool hangs after re-spawning several worker process. #76067
Multiprocessing's pool apparently attempts to repopulate the pool in an event of sub-process worker crash. However the pool seems to hangs after about ~ 4*(number of worker) process re-spawns.
I've tracked the issue down to queue.get() stalling at multiprocessing.pool, line 102
Is this a known issue? Are there any known workaround?
To reproduce this issue:
importmultiprocessingimportmultiprocessing.utilimportloggingmultiprocessing.util._logger=multiprocessing.util.log_to_stderr(logging.DEBUG)
importtimeimportctypesdefcrash_py_interpreter():
print("attempting to crash the interpreter in ", multiprocessing.current_process())
i=ctypes.c_char('a'.encode())
j=ctypes.pointer(i)
c=0whileTrue:
j[c] ='a'.encode()
c+=1jdeftest_fn(x):
print("test_fn in ", multiprocessing.current_process().name, x)
exit(0)
time.sleep(0.1)
if__name__=='__main__':
# pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())pool=multiprocessing.Pool(processes=1)
args_queue= [nforninrange(20)]
# subprocess quitspool.map(test_fn, args_queue)
Generally speaking, queues can remain in an inconsistent state after a process crash (because the process might have crashed just after acquiring a shared semaphore or sending part of a large message). It's not obvious to me how we could make them safer, at least under Unix where there's no widely-available message-oriented communication facility that I know of.
I hit this through the multiprocess fork of this module (uqfoundation/multiprocess#220), and I believe I've tracked down the cause, at least for the exit() case in the original report. It isn't related to how many workers have been respawned, and the queues aren't left in a bad state. The worker loop in Lib/multiprocessing/pool.py only catches Exception around the task:
So a SystemExit (from exit(), sys.exit(), or a SIGTERM handler that raises it) escapes the loop, and the worker process exits before it ever sends a result back. The pool replaces the worker, but that job's result never arrives, so map() / get() waits forever.
The "about 4x the number of workers" in the original report is just how map() chunks the work. With Pool(1) and 20 items it sends 4 chunks of 5, each chunk takes out its worker, so you see 4 respawns and then the hang. It still reproduces on main (3.16.0a1) and on 3.11-3.13, with fork, spawn and forkserver:
The way I hit it in practice was a worker that installs a SIGTERM handler raising SystemExit for graceful shutdown.
I have a fix: catch BaseException in the worker and send it back like any other exception, so get() raises it, the same way Future.result() does for ProcessPoolExecutor. Unlike ProcessPoolExecutor, the worker process then re-raises it once the result has been sent, so it still exits with the same exit code and the pool replaces it. I went that way rather than keeping the worker alive because a worker that swallows SystemExit hangs in a different way: if it's busy when pool.terminate() sends SIGTERM and its handler raises SystemExit, it goes back for more work and blocks on the queue lock that terminate() is holding. ThreadPool threads report the exception and keep running, since ThreadPool only replaces an exited thread when its worker handler happens to wake up, so jobs queued behind it would still hang.
This only covers tasks that raise. A worker that dies outright (os._exit(), a segfault like the commented-out ctypes case, SIGKILL or the OOM killer) still hangs the pool. That's the broader gh-66587, and Antoine's point about queue state applies there, so I'll leave it to you whether this issue should be closed by the fix. PR posted as well.
Activity
olarn commentedon Oct 27, 2017
Multiprocessing's pool apparently attempts to repopulate the pool in an event of sub-process worker crash. However the pool seems to hangs after about ~ 4*(number of worker) process re-spawns.
I've tracked the issue down to queue.get() stalling at multiprocessing.pool, line 102
Is this a known issue? Are there any known workaround?
To reproduce this issue:
pitrou commentedon Oct 29, 2017
Generally speaking, queues can remain in an inconsistent state after a process crash (because the process might have crashed just after acquiring a shared semaphore or sending part of a large message). It's not obvious to me how we could make them safer, at least under Unix where there's no widely-available message-oriented communication facility that I know of.
dxdc commentedon Oct 21, 2026
I hit this through the
multiprocessfork of this module (uqfoundation/multiprocess#220), and I believe I've tracked down the cause, at least for theexit()case in the original report. It isn't related to how many workers have been respawned, and the queues aren't left in a bad state. The worker loop inLib/multiprocessing/pool.pyonly catchesExceptionaround the task:So a
SystemExit(fromexit(),sys.exit(), or a SIGTERM handler that raises it) escapes the loop, and the worker process exits before it ever sends a result back. The pool replaces the worker, but that job's result never arrives, somap()/get()waits forever.The "about 4x the number of workers" in the original report is just how
map()chunks the work. WithPool(1)and 20 items it sends 4 chunks of 5, each chunk takes out its worker, so you see 4 respawns and then the hang. It still reproduces on main (3.16.0a1) and on 3.11-3.13, with fork, spawn and forkserver:The way I hit it in practice was a worker that installs a SIGTERM handler raising
SystemExitfor graceful shutdown.I have a fix: catch
BaseExceptionin the worker and send it back like any other exception, soget()raises it, the same wayFuture.result()does forProcessPoolExecutor. UnlikeProcessPoolExecutor, the worker process then re-raises it once the result has been sent, so it still exits with the same exit code and the pool replaces it. I went that way rather than keeping the worker alive because a worker that swallowsSystemExithangs in a different way: if it's busy whenpool.terminate()sends SIGTERM and its handler raisesSystemExit, it goes back for more work and blocks on the queue lock thatterminate()is holding.ThreadPoolthreads report the exception and keep running, sinceThreadPoolonly replaces an exited thread when its worker handler happens to wake up, so jobs queued behind it would still hang.This only covers tasks that raise. A worker that dies outright (
os._exit(), a segfault like the commented-out ctypes case, SIGKILL or the OOM killer) still hangs the pool. That's the broader gh-66587, and Antoine's point about queue state applies there, so I'll leave it to you whether this issue should be closed by the fix. PR posted as well.