Skip to content

fix(worker): force worker process exit after shutdown timeout - #732

Open
Devpaul-01 wants to merge 3 commits into
taskiq-python:masterfrom
Devpaul-01:fix/worker-forced-exit-after-shutdown
Open

Devpaul-01 wants to merge 3 commits into
taskiq-python:masterfrom
Devpaul-01:fix/worker-forced-exit-after-shutdown

Conversation

@Devpaul-01

Copy link
Copy Markdown

Addresses #646 (see also discussion #608).

Problem

When --max-tasks-per-child is reached, the worker stops prefetching, waits for the running tasks and logs Shutting down the broker.. If the process doesn't exit after that, ProcessManager never restarts it: it only restarts workers that are dead. The worker slot then stops consuming tasks for good, and the manager can't shut down either, because multiprocessing joins its non-daemon children at exit.

Root cause

Nothing guarantees that a worker process exits once its graceful shutdown is over. Two things in the broker can prevent it:

  1. A broker leaves a non-daemon thread behind. The interpreter waits for it at exit (threading._shutdown).
  2. broker.shutdown() blocks the event loop. shutdown_broker uses asyncio.wait_for(..., timeout), but a timeout can only fire if the loop is free to run callbacks, so a blocking shutdown() defeats it.

Solution

When the shutdown of a worker process starts, arm a daemon watchdog (threading.Timer). If the process hasn't exited within --shutdown-timeout plus a 1 second grace period, it logs a warning and calls os._exit(0), so the manager sees a dead worker and restarts it.

  • A daemon thread doesn't delay a process that exits in time, and it keeps running while the interpreter waits for non-daemon threads or the event loop is blocked. A healthy worker exits long before the deadline, and the watchdog never fires.
  • It is armed only when multiprocessing.parent_process() is not None, i.e. in a child of the process manager. start_listen can also be called directly, and that process must never be killed.
  • No new CLI flag: the existing --shutdown-timeout is reused.
  • The arming lives in a small shutdown_worker wrapper around shutdown_broker, so start_listen keeps its size and complexity.

Trade-offs

os._exit skips atexit handlers and interpreter finalization. It runs only after the graceful shutdown has already had its full timeout, and only for a process that wouldn't exit anyway. If you prefer a different exit path, a different grace period or a dedicated flag, I'm happy to change it.

Tests

  • Reproduced with the real taskiq worker CLI and a minimal broker that leaves a non-daemon thread behind: after Shutting down the broker. the child stayed alive for over a minute, with no restart.
  • tests/cli/worker/test_worker_exit.py runs the worker CLI under every available start method (fork, spawn, forkserver) with a broker that either leaves a non-daemon thread behind or blocks the event loop in shutdown(), and checks that the worker is restarted. The same test fails without the fix: the restart never happens and the test times out.
  • A worker that exits on its own is restarted without the forced exit, and force_exit_after has a unit test.
  • Checked on Linux with Python 3.12: the full suite passes (411 passed), as do ruff and black on the changed files and mypy on run.py. ruff reports 3 errors elsewhere in the package (e.g. taskiq/instrumentation.py) that are also reported without my changes.

Not verified

  • I didn't run this against taskiq-redis. The brokers in the tests are synthetic, they reproduce the symptom but I haven't confirmed which of the two causes affects taskiq-redis. The fix is independent of that.
  • Not run on Windows or macOS. The new CLI tests are skipped on Windows because they rely on process groups.

A worker process can stay alive after its graceful shutdown finished: a
broker may leave a non-daemon thread behind, or block the event loop in
shutdown(), which makes the asyncio.wait_for timeout in shutdown_broker
unable to fire. The process manager restarts only dead workers, so such a
process is never replaced. For example, a worker that reached
--max-tasks-per-child stopped consuming tasks and its slot was lost.

Arm a daemon watchdog when the shutdown starts and terminate the process
if it didn't exit within shutdown_timeout plus a short grace period. The
watchdog is armed only in child processes of the process manager, so
start_listen called directly never kills the current process.
Run the worker CLI with a broker that prevents the process from exiting
after --max-tasks-per-child is reached, either by leaving a non-daemon
thread behind or by blocking the event loop in shutdown(), and check that
the process manager restarts the worker. Also check that a worker that
exits on its own is not terminated, and the watchdog itself.

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.

1 participant