Skip to content

fix: drain_grace_seconds bounds a serve() that is still running after stop - #91

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/88-bound-serve-tail
Oct 16, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/88-bound-serve-tail

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Summary

drain_grace_seconds is documented as the time each entrypoint gets for in-flight work. That held for servers, whose requests finish inside drain(). It did not hold for scoped entrypoints. A consumer's batch, a daemon's iteration or a one-shot function runs inside serve(), and the Host awaited every serve() 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 stayed True the whole time, and no DrainTimeoutError was raised.

Now, once stop is set, Host._serve:

  • withdraws readiness right away. Nothing has stopped accepting at that point, so this is the same "unready, still accepting" state as before, only earlier;
  • gives every serve() still running drain_grace_seconds + 5 s, the same allowance a drain() step gets;
  • cancels a serve() that overruns, logs Shutdown step timed out with phase=serve and the entrypoint's kind, and hands a DrainTimeoutError to the existing timeouts list. It is raised from run() only when nothing else is propagating.

drain_delay_seconds, drain(), stop() and the hooks keep their order and budgets. When every serve() returns on its own without stop (all entrypoints non-essential), the Host still goes straight to shutdown as before. A test pins that.

Same reporter script after the change:

health.ready=False,  1.0s after stop
run() raised DrainTimeoutError: serve did not return within 5.5s of stop,  5.5s after stop

Design

The model is shared with #89: AppSpec.drain_grace_seconds is the one budget for an entrypoint's in-flight work, wherever that work runs, and transport-level knobs can only shorten it.

Rejected:

  • A separate setting for the serve tail. The docs already promise drain_grace_seconds for this work, so a second knob would only give people another way to get it wrong.
  • Per-entrypoint shutdown pipelines, so an HTTP server could drain while a consumer is still finishing its batch. That would rewrite the ordering contract, and making the budget true doesn't need it.
  • Documenting the unbounded tail instead. The Kubernetes arithmetic would then need an open-ended term.

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 needs drain_grace_seconds twice. The lifecycle and Kubernetes pages now say so.

Behaviour changes worth a look

  • A OneShotEntrypoint or DaemonEntrypoint interrupted by SIGTERM is cancelled after drain_grace_seconds + 5 s (35 s by default) instead of running to completion. Under Kubernetes nothing changes in practice, because the kubelet's SIGKILL used 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 cancels serve() at the next await point", and now the code does that.
  • Readiness goes False when stop is set, not when the slowest serve() returns. For a service made only of servers nothing changes, because their serve() 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_finishing is True), passes here;
  • serve_returns_within_the_drain_grace_after_stop__lets_the_unit_finish and every_non_essential_entrypoint_returns__shuts_down_without_a_stop: guards, green before and after.

make check and make test pass (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=serve vs phase=drain), agents page, and the AppSpec.drain_grace_seconds and Entrypoint.serve docstrings.

Closes #88

… 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
AlexeyShalaev merged commit 5011e4d into master Oct 16, 2026
7 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/88-bound-serve-tail branch October 16, 2026 14:55
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
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.

drain_grace_seconds does not bound in-flight work in ScopedEntrypoint/DaemonEntrypoint: the Host awaits serve() with no limit

1 participant