Skip to content

fix(dispatcher): the in-process arm had no drain — its flag was never set - #590

Merged
athvin merged 1 commit into
mainfrom
bm/bluegreen-p14-dispatcher-drain
Nov 5, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/bluegreen-p14-dispatcher-drain

Conversation

@athvin

@athvin athvin commented Nov 5, 2026

Copy link
Copy Markdown
Owner

main.rs passed run_loop a local AtomicBool::new(false) named never. The name was accurate: nothing ever set it, so with ER_DISPATCH_IN_PROCESS=1 the dispatcher had no SIGTERM drain at all, while the standalone er-dispatcher did. dispatcher_bin.rs's own header recorded the difference as a known gap.

Two defects, and the second is the one that mattered

Setting the flag is only half a drain. The task is tokio::spawned, so main returning drops the runtime and kills whatever the dispatcher was mid-way through — the exact outcome the flag exists to prevent. So main now holds the JoinHandle and awaits it, after the server's own drain completes.

Unbounded on purpose. This path is only set by frontend/dev/env.sh: there's no kubelet here to SIGKILL past a grace period, and a process that exits when both halves are done is what a developer wants. The deployed dispatcher is its own Deployment with its own terminationGracePeriodSeconds (#443).

One drain, two shapes

shutdown::sigterm_flag() now serves both arms instead of being written out twice. on_sigterm() is the shape axum::serve wants — a future. This is the shape a loop wants — a value it polls at a boundary of its own choosing, so it finishes the job in hand. Same signal, two shapes.

Registering both in one process is safe, and the log proves it

Rather than asserting it from the docs — tokio::signal hands every caller an independent stream and a delivered signal wakes all of them:

16:18:49.938849  SIGTERM received; draining in-flight requests   <- server
16:18:49.938849  SIGTERM received; draining the in-flight job    <- dispatcher
16:18:49.939071  waiting for the in-process dispatcher to drain
16:18:49.999932  the dispatcher is draining and will exit
process exit=0

Same microsecond for both handlers, so neither consumed the signal from the other.

And that last line could not have appeared before this change: with the flag false forever the loop never took that branch. Its presence is the fix working, not a message that was always printed.

Two comments corrected

Both became false, so they're fixed here rather than left to mislead:

  • dispatcher_bin's header no longer warns about a difference that's gone.
  • main.rs no longer claims the merged web-api service runs the dispatcher in-process with a separate Deployment as "the alternative topology" — the §6.7 split inverted that, and the chart has shipped a separate er-dispatcher Deployment since.

Verification

  • make check green.
  • The live SIGTERM run above, against a throwaway Postgres, with /readyz returning 200 first so the process was genuinely serving when signalled.

Last item of the blue/green plan.

🤖 Generated with Claude Code

… set

`main.rs` passed `run_loop` a local `AtomicBool::new(false)` named `never`. The
name was accurate: nothing ever set it, so with `ER_DISPATCH_IN_PROCESS=1` the
dispatcher had no SIGTERM drain at all while the standalone `er-dispatcher`
did. `dispatcher_bin.rs`'s own header recorded the difference as a known gap.

**Two defects, and the second is the one that mattered.** Setting the flag is
only half a drain: the task is `tokio::spawn`ed, so `main` returning drops the
runtime and kills whatever the dispatcher was mid-way through — the exact
outcome the flag exists to prevent. So `main` now holds the `JoinHandle` and
awaits it after the server's own drain completes.

Unbounded on purpose: this path is only set by `frontend/dev/env.sh`, where
there is no kubelet to SIGKILL past a grace period and a process that exits
when both halves are done is what a developer wants. The deployed dispatcher is
its own Deployment with its own `terminationGracePeriodSeconds`.

`shutdown::sigterm_flag()` is the drain in both arms now rather than written
out twice — `on_sigterm()` is the shape `axum::serve` wants (a future), this is
the shape a loop wants (a value it polls at a boundary of its choosing). Same
signal, two shapes.

**Registering both in one process is safe, and the log proves it** rather than
the docs asserting it. `tokio::signal` hands every caller an independent stream
and a delivered signal wakes all of them:

    16:18:49.938849  SIGTERM received; draining in-flight requests   <- server
    16:18:49.938849  SIGTERM received; draining the in-flight job    <- dispatcher
    16:18:49.939071  waiting for the in-process dispatcher to drain
    16:18:49.999932  the dispatcher is draining and will exit
    process exit=0

Same microsecond for both handlers, so neither consumed the signal from the
other. That last line could NOT have appeared before this change: with the flag
false forever the loop never took that branch, so its presence is the fix
working rather than a message that was always printed.

Also corrected two comments that this makes false, rather than leaving them to
mislead: `dispatcher_bin`'s header no longer warns about a difference that is
gone, and `main.rs` no longer claims the merged `web-api` service runs the
dispatcher in-process with a separate Deployment as "the alternative topology"
— the §6.7 split inverted that, and the chart has shipped a separate
`er-dispatcher` Deployment since.

Verified: `make check` green, plus the live SIGTERM run above against a
throwaway Postgres (`/readyz` 200 first, so the process was really serving when
it was signalled).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants