Skip to content

Shut down promptly when the kernel has a multiprocessing resource tracker - #1607

Open
mokashang wants to merge 1 commit into
ipython:mainfrom
mokashang:fix/shutdown-resource-tracker-zombies
Open

mokashang wants to merge 1 commit into
ipython:mainfrom
mokashang:fix/shutdown-resource-tracker-zombies

Conversation

@mokashang

Copy link
Copy Markdown

Summary

A graceful kernel shutdown takes ~29 s on Python 3.14 (and on earlier CPython under forkserver/spawn) if the kernel ever created a multiprocessing synchronization primitive, which is ordinary code: tqdm builds a multiprocessing.RLock for every progress bar, for example. That is longer than jupyter_client's default shutdown_wait_time=5, so the kernel is SIGTERM-ed mid-shutdown and atexit handlers (coverage.py subprocess data flush, etc.) never run.

Two independent causes stack in _progressively_terminate_all_children, both in ipykernel/kernelbase.py:

  1. multiprocessing spawns a resource tracker child the first time a SemLock is created with forkserver/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.
  2. Nothing in Kernel calls waitpid. After we fall through to SIGKILL the tracker becomes a zombie, and psutil.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_children calls a new _stop_multiprocessing_resource_tracker helper before signalling children. The helper closes the tracker's pipe and waitpid-s it through multiprocessing.resource_tracker._resource_tracker._stop() — the same stop multiprocessing registers at atexit. The call is guarded to be a no-op when no tracker was ever started.
  • _process_children filters 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

  • Added test_shutdown_fast_with_multiprocessing_resource_tracker in tests/test_kernel.py: creates a forkserver Lock in the kernel, then shuts it down with shutdown_wait_time=60 and 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.
  • On macOS/Python 3.13 with 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) and test_child_process_fallbacks_without_psutil all still pass; the wider suite (tests/test_kernel*.py, subshells, embed, iostream, etc.) is green locally — 201 passed, 14 skipped.
  • ruff check and ruff format clean on the touched files.

Fixes #1601

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graceful shutdown takes ~29 s when the kernel has a multiprocessing resource tracker child (default on Python 3.14)

1 participant