Repository navigation
Conversation
|
The code mirrors the stdlib |
|
@mmckerns ok thanks. I'm not sure how to get the tests to pass, as I'm not very familiar with this library. but these tests definitely seem to hang. def test_map_sigterm_base_exception(self):
def _sigterm_worker(x):
def _handler(signum, frame):
raise SystemExit(f"worker received SIGTERM on input={x}")
signal.signal(signal.SIGTERM, _handler)
os.kill(os.getpid(), signal.SIGTERM)
try:
self.pool.map_async(_sigterm_worker, list(range(3))).get(timeout=SHUTDOWN_TIMEOUT)
except multiprocessing.TimeoutError:
self.fail("pool.map_async with sigterm stalled on base exception")
def test_imap_sigterm_base_exception(self):
def _sigterm_worker(x):
def _handler(signum, frame):
raise SystemExit(f"worker received SIGTERM on input={x}")
signal.signal(signal.SIGTERM, _handler)
os.kill(os.getpid(), signal.SIGTERM)
try:
for _ in self.pool.imap(_sigterm_worker, list(range(3))):
pass
except multiprocessing.TimeoutError:
self.fail("pool.map_async with sigterm stalled on base exception")
|
|
@mmckerns any chance you could review this again? I made the other changes |
|
@mmckerns just bumping this in case it got buried — whenever you have a chance, I’d appreciate another look. Thanks! |
|
@dxdc: If you look at the current version of |
…n#220) A task that raised SystemExit or KeyboardInterrupt (e.g. from a SIGTERM handler) killed its worker before any result was sent back, so the caller waited forever on the lost task. The worker now catches BaseException and sends it back like any other exception. In a worker process, SystemExit/KeyboardInterrupt is then re-raised so the worker still exits as before and the pool replaces it. Keeping the worker alive instead (as ProcessPoolExecutor does) would leave a busy worker that turns SIGTERM into SystemExit unable to be stopped by Pool.terminate(). ThreadPool threads just report the exception, since the pool does not notice a worker thread exiting. Applied to py3.10-3.15 and pypy3.10-3.12, with tests added to each version's _TestPoolWorkerErrors. Fixes uqfoundation#220
|
@mmckerns thanks, that makes sense. I've rebased onto current master and applied the change to every version directory there now (py3.10-3.15 and pypy3.10-3.12, py3.9 dropped since master no longer has it), with tests added to each version's I also changed the fix itself. Just catching So now the worker sends the exception back and then re-raises it, i.e. it exits exactly as it did before and the pool replaces it, the only difference being that the caller gets the error instead of waiting forever. I ran the full suite on 3.11, 3.12 and 3.13 with fork, spawn and forkserver, and ran the new tests several hundred times under load to make sure they aren't flaky. If you'd rather keep this as close to the stdlib as possible I'm happy to trim it back to the one-line change, though the |
|
@dxdc thanks for the follow-up. I think, as this is really a python stdlib bug, that you should probably also submit a PR there. That way, both libraries stay in sync as much as possible -- although it would get applied in |
|
Makes sense, I'll open an issue on CPython and put up a PR there with the same change, and link both back here. I checked and the stdlib hangs the same way on 3.11-3.13 with both fork and spawn, so it's definitely upstream too. One thought: Pool robustness fixes have historically moved pretty slowly in CPython (python/cpython#66587 has been open since 2014), so if it's ok with you I'd suggest not blocking this one on it. If CPython ends up wanting a different shape for the fix, I'll update this PR to match so the two stay in sync. |
|
I wasn't suggesting waiting to merge here until after upstream merges. However, I definitely think you should report upstream -- and we can wait a slight bit to see of there's some comments about rationale and/or preferred fixes and/or tag classification. |
|
posted here: python/cpython#160029 |
Summary
Fixes #220
A task that raises
SystemExitorKeyboardInterrupt(for example from a SIGTERM handler, as in #220) kills its pool worker before any result is sent back, so the caller waits forever on the lost task.The worker now catches
BaseExceptionand sends it back like any other exception. In a worker process,SystemExit/KeyboardInterruptis then re-raised, so the worker still exits as before and the pool replaces it. Keeping the worker alive instead (asProcessPoolExecutordoes) would makePool.terminate()hang on a busy worker that turns SIGTERM intoSystemExit.ThreadPoolthreads just report the exception, since the pool doesn't notice a worker thread exiting.Applied to py3.10-3.15 and pypy3.10-3.12, with tests in each version's
_TestPoolWorkerErrors. The full suite passes on 3.11, 3.12 and 3.13 with fork, spawn and forkserver.Checklist
Documentation and Tests
python tests/__main__.py, and pass.Release Management
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_015WHEYyvQhEKEYvBtjyfiNr
Checklist
Documentation and Tests
python tests/__main__.py, and pass.Release Management