Repository navigation
fix(worker): force worker process exit after shutdown timeout - #732
Open
Devpaul-01 wants to merge 3 commits into
Open
Devpaul-01 wants to merge 3 commits into
Devpaul-01 wants to merge 3 commits into
Conversation
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
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.
Addresses #646 (see also discussion #608).
Problem
When
--max-tasks-per-childis reached, the worker stops prefetching, waits for the running tasks and logsShutting down the broker.. If the process doesn't exit after that,ProcessManagernever 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, becausemultiprocessingjoins 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:
threading._shutdown).broker.shutdown()blocks the event loop.shutdown_brokerusesasyncio.wait_for(..., timeout), but a timeout can only fire if the loop is free to run callbacks, so a blockingshutdown()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-timeoutplus a 1 second grace period, it logs a warning and callsos._exit(0), so the manager sees a dead worker and restarts it.multiprocessing.parent_process()is notNone, i.e. in a child of the process manager.start_listencan also be called directly, and that process must never be killed.--shutdown-timeoutis reused.shutdown_workerwrapper aroundshutdown_broker, sostart_listenkeeps its size and complexity.Trade-offs
os._exitskipsatexithandlers 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
taskiq workerCLI and a minimal broker that leaves a non-daemon thread behind: afterShutting down the broker.the child stayed alive for over a minute, with no restart.tests/cli/worker/test_worker_exit.pyruns 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 inshutdown(), and checks that the worker is restarted. The same test fails without the fix: the restart never happens and the test times out.force_exit_afterhas a unit test.ruffandblackon the changed files andmypyonrun.py.ruffreports 3 errors elsewhere in the package (e.g.taskiq/instrumentation.py) that are also reported without my changes.Not verified
taskiq-redis. The brokers in the tests are synthetic, they reproduce the symptom but I haven't confirmed which of the two causes affectstaskiq-redis. The fix is independent of that.