Repository navigation
Conversation
…cker The multiprocessing resource tracker is a child process spawned the first time a synchronization primitive (Lock, Semaphore, Event, ...) is created under the ``forkserver`` or ``spawn`` start methods. Python 3.14 makes ``forkserver`` the default on Linux, so creating one of these primitives is ordinary code; ``tqdm`` builds a ``multiprocessing.RLock`` on every progress bar, for example. The tracker ignores SIGINT and SIGTERM by design and only exits when its control pipe is closed. In ``_progressively_terminate_all_children`` the SIGTERM schedule therefore runs to completion (~14 s) even though every other child has exited, and the tracker is still reported as a child by ``psutil`` after we fall through to SIGKILL (as a zombie, until the parent waitpid-s it), which stalls the SIGKILL schedule for another ~14 s. A graceful shutdown of a kernel that touched a lock then takes ~29 s, which is longer than ``jupyter_client``'s default ``shutdown_wait_time`` of 5 s, so the kernel is SIGTERM-ed mid-shutdown and ``atexit`` handlers (for example coverage.py subprocess data flush) never run. Fix both halves: * ``_progressively_terminate_all_children`` now calls ``multiprocessing.resource_tracker._resource_tracker._stop()`` before signalling children. That is the same stop that multiprocessing runs at interpreter exit; it closes the pipe and waitpid-s the tracker, so the SIGTERM round no longer waits for a process that will never react to SIGTERM. The call is guarded to be a no-op when no tracker was ever started. * ``_process_children`` filters out zombie children. Nothing in this class calls waitpid, so a zombie would otherwise keep the loop running out its schedule after the signal that killed it has already landed; init reaps them once the kernel exits. Fixes ipython#1601
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A graceful kernel shutdown takes ~29 s on Python 3.14 (and on earlier CPython under
forkserver/spawn) if the kernel ever created amultiprocessingsynchronization primitive, which is ordinary code:tqdmbuilds amultiprocessing.RLockfor every progress bar, for example. That is longer thanjupyter_client's defaultshutdown_wait_time=5, so the kernel is SIGTERM-ed mid-shutdown andatexithandlers (coverage.py subprocess data flush, etc.) never run.Two independent causes stack in
_progressively_terminate_all_children, both inipykernel/kernelbase.py:multiprocessingspawns a resource tracker child the first time aSemLockis created withforkserver/spawn. That child is in the kernel's process group, installs handlers that ignore SIGINT and SIGTERM, and only exits when its control pipe is closed. The SIGTERM schedule in the shutdown loop sums to ~14.44 s, so the tracker keeps showing up after every sleep until the whole schedule has run.Kernelcallswaitpid. After we fall through to SIGKILL the tracker becomes a zombie, andpsutil.Process().children(recursive=True)still returns zombies. The SIGKILL schedule then also runs to completion before the loop returns.Fix
Two changes, matching the issue's analysis (#1601):
_progressively_terminate_all_childrencalls a new_stop_multiprocessing_resource_trackerhelper before signalling children. The helper closes the tracker's pipe andwaitpid-s it throughmultiprocessing.resource_tracker._resource_tracker._stop()— the same stop multiprocessing registers atatexit. The call is guarded to be a no-op when no tracker was ever started._process_childrenfilters out zombie children. They cannot make progress toward exit from the kernel's side, and init reaps them once the kernel's own process exits.Testing
test_shutdown_fast_with_multiprocessing_resource_trackerintests/test_kernel.py: creates aforkserverLockin the kernel, then shuts it down withshutdown_wait_time=60and asserts the graceful shutdown completes in <10 s. The 10 s cap is a generous ceiling; before the fix the SIGTERM schedule alone forces ~14 s.forkserver, the reproducer from Graceful shutdown takes ~29 s when the kernel has a multiprocessing resource tracker child (default on Python 3.14) #1601 drops from 14.8 s to 0.1 s.test_shutdown(plain kernel),test_shutdown_subprocesses(child-process group semantics) andtest_child_process_fallbacks_without_psutilall still pass; the wider suite (tests/test_kernel*.py, subshells, embed, iostream, etc.) is green locally — 201 passed, 14 skipped.ruff checkandruff formatclean on the touched files.Fixes #1601