Repository navigation
fix: drain_grace_seconds bounds a serve() that is still running after stop - #91
Merged
Merged
Conversation
… stop A scoped entrypoint (consumer, daemon, one-shot) finishes its in-flight unit inside serve(), and the Host awaited every serve() with no deadline. That work ran outside any budget, DrainTimeoutError never fired, and readiness stayed true until the slowest serve() returned. Once stop is set the Host now withdraws readiness and gives every serve() still running drain_grace_seconds + 5 s to return, the same allowance as a drain() step. A serve() still running after that is cancelled, its kind is logged, and a DrainTimeoutError goes through the existing timeouts path. Closes #88
AlexeyShalaev
added a commit
that referenced
this pull request
Oct 17, 2026
… stop (#91) A scoped entrypoint (consumer, daemon, one-shot) finishes its in-flight unit inside serve(), and the Host awaited every serve() with no deadline. That work ran outside any budget, DrainTimeoutError never fired, and readiness stayed true until the slowest serve() returned. Once stop is set the Host now withdraws readiness and gives every serve() still running drain_grace_seconds + 5 s to return, the same allowance as a drain() step. A serve() still running after that is cancelled, its kind is logged, and a DrainTimeoutError goes through the existing timeouts path. Closes #88
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
drain_grace_secondsis documented as the time each entrypoint gets for in-flight work. That held for servers, whose requests finish insidedrain(). It did not hold for scoped entrypoints. A consumer's batch, a daemon's iteration or a one-shot function runs insideserve(), and the Host awaited everyserve()with no deadline. With the reproduction from #88 (drain_grace_seconds=0.5, an 8 s batch in flight),run()returned normally after 7.5 s, readiness stayedTruethe whole time, and noDrainTimeoutErrorwas raised.Now, once
stopis set,Host._serve:serve()still runningdrain_grace_seconds + 5 s, the same allowance adrain()step gets;serve()that overruns, logsShutdown step timed outwithphase=serveand the entrypoint'skind, and hands aDrainTimeoutErrorto the existingtimeoutslist. It is raised fromrun()only when nothing else is propagating.drain_delay_seconds,drain(),stop()and the hooks keep their order and budgets. When everyserve()returns on its own withoutstop(all entrypoints non-essential), the Host still goes straight to shutdown as before. A test pins that.Same reporter script after the change:
Design
The model is shared with #89:
AppSpec.drain_grace_secondsis the one budget for an entrypoint's in-flight work, wherever that work runs, and transport-level knobs can only shorten it.Rejected:
drain_grace_secondsfor this work, so a second knob would only give people another way to get it wrong.The steps run one after another (all serve tails together, then each
drain()in turn), so a pod whose consumer and HTTP server can both be busy for the whole window needsdrain_grace_secondstwice. The lifecycle and Kubernetes pages now say so.Behaviour changes worth a look
OneShotEntrypointorDaemonEntrypointinterrupted bySIGTERMis cancelled afterdrain_grace_seconds + 5 s(35 s by default) instead of running to completion. Under Kubernetes nothing changes in practice, because the kubelet'sSIGKILLused to end it anyway, just later and with no log. Outside Kubernetes (systemd, a terminal), a batch that used to finish after Ctrl+C is now cut off at the window. The batch-job blueprint already said "the Host cancelsserve()at the next await point", and now the code does that.Falsewhenstopis set, not when the slowestserve()returns. For a service made only of servers nothing changes, because theirserve()returns at once.I'm calling this a fix, not a breaking change: it makes the code do what the docs and docstrings already promised.
Tests
New in
tests/unit/test_host.py:serve_outlasts_the_drain_grace_after_stop__cancels_it_and_raises_drain_timeout_error: fails on master (the run hangs into the 5 s test guard), passes here;serve_still_finishing_its_unit_after_stop__readiness_is_already_withdrawn: fails on master (ready_while_finishingisTrue), passes here;serve_returns_within_the_drain_grace_after_stop__lets_the_unit_finishandevery_non_essential_entrypoint_returns__shuts_down_without_a_stop: guards, green before and after.make checkandmake testpass (787 passed, 93.24 % coverage), and the docs build reports no issues.Docs
Lifecycle page (shutdown steps, budgets table, sequence diagram, Kubernetes tip), entrypoints page, custom-entrypoint checklist, worker and batch-job blueprints, Kubernetes page, runbook (
phase=servevsphase=drain), agents page, and theAppSpec.drain_grace_secondsandEntrypoint.servedocstrings.Closes #88