Skip to content

Keep serving for a while after SIGTERM (Config.shutdownDelayMs) - #8427

Merged
piscisaureus merged 3 commits into
mainfrom
piscisaureus/shutdown-delay
Nov 24, 2026
Merged

piscisaureus merged 3 commits into
mainfrom
piscisaureus/shutdown-delay

Conversation

@piscisaureus

@piscisaureus piscisaureus commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Behind a cloud load balancer, Kubernetes keeps sending new connections
to a terminating pod for up to about 20 s, because removing the
endpoint propagates at the same time as the SIGTERM. A workerd that
drains at once refuses them. Today the only fix is a preStop sleep in
the manifest; this lets workerd cover it itself.

  • Config.shutdownDelayMs (default 0, which drains at once as before)
    makes the first SIGTERM log one line and keep serving for that long,
    then run the existing drain unchanged. WORKERD_SHUTDOWN_DELAY_MS
    overrides the field when set, for wrangler.json users and for
    deployments that adjust a baked config. serve --help lists the
    variable.
  • WORKERD_CLUSTER_STOP_TIMEOUT_MS likewise overrides
    ClusterConfig.stopTimeoutMs, also for a cluster joined with
    --cluster-addr, so the two timings that the grace period must cover
    are set in one place.
  • Each variable must be a whole number of milliseconds up to an hour;
    blank counts as unset, and anything else, a non-UTF-8 value included,
    ends the process at startup with an error that names it. Non-UTF-8
    cluster key and bucket variables now do too, as does a configured
    delay above an hour. A deployment that sets shutdownDelayMs is
    refused.
  • A cluster node keeps its lease, membership and Durable Objects
    through the delay. /ready on the cluster port answers 503 from the
    first SIGTERM on. Only readiness probes read it; peers keep reaching
    the node's objects.
  • A SIGTERM before the server serves drains it at once: it was never
    ready, so nothing sends it traffic. A second SIGTERM during the delay
    drains at once. SIGINT is not caught, so Ctrl-C still stops the
    process immediately.
  • ClusterConfig.stopTimeoutMs runs from the end of the delay, so the
    grace period must cover both: 20 s + 8 s fits in 40 s.
  • As with a preStop sleep, objects first requested from the node
    during the delay join its handoff at the end.

On a three-pod GKE demo with a 20 s delay and no preStop, two
rollouts served 801 and 845 requests, each on a new connection, with no
failures. Without preStop they failed 2 to 14 requests.

The Kubernetes sample in #8274 still uses a preStop sleep and can
switch to these variables once both have landed.

Kind Added Removed Where
Implementation 261 58 run.rs, entry.rs, cluster.rs, CLI config
Tests 573 0 cluster, config, server and compose tests
Documentation 71 18 workerd.capnp, serve help, AGENTS.md

Comments in code files are counted with that file, except the
workerd.capnp comments, which are the config reference, and the help
text.

@piscisaureus
piscisaureus requested review from a team as code owners November 13, 2026 05:07
@piscisaureus
piscisaureus requested a review from ry November 13, 2026 05:07

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 085f652b.

This review, of f57bbf8b

💬 Approved with comments — The shutdown-delay logic looks correct and is well tested. The only note is an INFO about unrelated changes to MODULE.bazel.lock, including a hermetic_launcher downgrade.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

The shutdown-delay logic looks correct; MODULE.bazel.lock has unrelated changes.

Recommendation: 💬 Optional Nits

  • [INFO] MODULE.bazel.lock has changes unrelated to the feature, including a hermetic_launcher downgrade from 0.0.17 to 0.0.16. (MODULE.bazel.lock:145)
View full review

The timing logic is correct: the phases run in the right order, startup time counts towards the delay, a second SIGTERM ends the delay early, a zero delay drains at once, and /ready fails from the first signal on. The sigterm stream still waits forever when the signal handler cannot be installed, as the old code did.

MODULE.bazel.lock has changes unrelated to the feature: hermetic_launcher goes from 0.0.17 to 0.0.16, aspect_tools_telemetry goes from 0.5.1 to 0.5.2, and the yq.bzl usagesDigest changes. This looks like a lockfile regenerated against a different base.


✅ All other checks passed

🔒 Security: ✅ LGTM

The flag takes whole seconds as a u32. The change adds no new network endpoint, and a second SIGTERM always drains at once.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: f57bbf8b

Comment thread MODULE.bazel.lock Resolved
@codecov-commenter

codecov-commenter commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 143 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.75%. Comparing base (66154be) to head (9369cdc).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/workerd/server/cli/config.rs 0.00% 69 Missing ⚠️
src/workerd/server/server/run.rs 0.00% 32 Missing ⚠️
src/workerd/server/cli/lib.rs 0.00% 19 Missing ⚠️
src/workerd/server/server/entry.rs 0.00% 12 Missing ⚠️
src/workerd/server/server/cluster.rs 0.00% 5 Missing ⚠️
src/workerd/server/server/in_process.rs 0.00% 5 Missing ⚠️
src/workerd/server/cluster/compose.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8427      +/-   ##
==========================================
- Coverage   67.78%   67.75%   -0.04%     
==========================================
  Files         848      848              
  Lines      215530   215648     +118     
  Branches    32508    32508              
==========================================
+ Hits       146103   146105       +2     
- Misses      50228    50343     +115     
- Partials    19199    19200       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from f57bbf8 to 085f652 Compare November 13, 2026 21:06

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of f1199141.

This review, of 085f652b

✅ Approved — No issues found. The only earlier note, about unrelated lockfile changes, is resolved because the lockfile is no longer changed.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

The signal timing, the factory ownership at shutdown, listeners that end early, the zero delay and the end of the signal stream are all handled correctly. The tests cover the delay, a signal during startup, a second signal, and /ready answering 503 while Durable Objects stay served. MODULE.bazel.lock is no longer part of the diff.


⚡ Performance: ✅ LGTM

The delay adds one timer and one stream poll, only on the drain path. No per-request cost.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 085f652b

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of a6272134.

This review, of f1199141

✅ Approved — No issues found. The only earlier note, about unrelated lockfile changes, is resolved because the lockfile is no longer changed.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

The signal timing, the factory ownership at shutdown, listeners that end early, the zero delay and the end of the signal stream are all handled correctly. The tests cover the delay, a signal during startup, a second signal, and /ready answering 503 while Durable Objects stay served. MODULE.bazel.lock is no longer part of the diff.


⚡ Performance: ✅ LGTM

The delay adds one timer and one stream poll, only on the drain path. No per-request cost.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: f1199141 — same changes as 085f652b, so this is that commit's review (no new review was run; line numbers are as of 085f652b)

@piscisaureus
piscisaureus requested a review from jasnell November 14, 2026 00:22
@ry

ry commented Nov 16, 2026

Copy link
Copy Markdown
Member

[AI] Fine with the feature, but I'd rather not add a CLI flag for it. Could it be a capnp field instead (top-level Config.shutdownDelayMs, since it applies outside clusters), with an env var that fills it for wrangler.json users, the way #8274 does WORKERD_CLUSTER_BUCKET and WORKERD_CLUSTER_KEY? Most users won't see the capnp, so the env var is how they'd reach it, and the help already lists the cluster env vars. I'd do the same for stopTimeoutMs (WORKERD_CLUSTER_STOP_TIMEOUT_MS), which wrangler.json can't set today, so the delay and the stop timeout, which have to fit in one grace period, are set in the same place.

Separately: a node signalled before it serves was never ready, so it could skip the delay. That would remove the timestamped drain stream, start_noting_drain, until_drain, the in-process mpsc and the beforeStart hook.

@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from f119914 to a627213 Compare November 16, 2026 23:31
@piscisaureus piscisaureus changed the title Keep serving for a while after SIGTERM with --shutdown-delay Keep serving for a while after SIGTERM (Config.shutdownDelayMs) Nov 16, 2026
@piscisaureus

Copy link
Copy Markdown
Contributor Author

Done, both points:

  • No flag: it's Config.shutdownDelayMs now, filled from
    WORKERD_SHUTDOWN_DELAY_MS when the config leaves it at 0, in
    cli/config.rs next to where Add a sample that runs a cluster on Kubernetes #8274 puts the cluster variables. A bad
    value, or a delay over an hour from either source, fails at startup.
  • A SIGTERM before the server serves drains at once. The timestamped
    stream, start_noting_drain, until_drain, the mpsc and the
    beforeStart hook are gone. Checking for that signal needed one
    yield_now() after start(): a start that never waits returns before
    tokio has polled its signal driver. A new cluster test with a slow
    starting worker fails without it.

WORKERD_CLUSTER_STOP_TIMEOUT_MS belongs with the other cluster
variables, so I'd add it in #8274 rather than here.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 1db663d5.

This review, of a6272134

✅ Approved — No issues found in the changes since the last review: the config field, the environment variable and the signal handling during the delay all behave as described, and tests cover them.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

WORKERD_SHUTDOWN_DELAY_MS must be digits only, and both it and the config value are limited to one hour. A bad value ends the process at startup. /ready answering 503 does not change any access control.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

Startup signal check

In run.rs, a yield_now() followed by now_or_never() sees a SIGTERM that arrived during startup. No signal is lost before run polls.

Signal handling during the delay

The /ready guard is dropped on the first signal. The delay and a second signal are raced, and start_draining runs only afterwards, so stopTimeoutMs counts from the end of the delay.

Config value

with_shutdown_delay_environment checks the one-hour limit on the configured value. It reads the variable only when the field is 0, and it keeps the rest of the config when it rewrites the root. It runs before the deployments are applied, so a deployment that sets shutdownDelayMs is still refused.

Tests

The tests cover each of these paths.


⚡ Performance: ✅ LGTM

The delay adds one timer and one stream poll, only when the process is shutting down. Nothing is added per request.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: a6272134

@ry ry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from a627213 to 1db663d Compare November 21, 2026 01:29

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 96685e52.

This review, of 1db663d5

💬 Approved with comments — The shutdown delay, the environment variable and the /ready handling all work as described, and tests cover them. The one note is an INFO: MODULE.bazel.lock is still changed in this pull request even though it has nothing to do with the feature.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

The feature code is correct and tested. One INFO: an unrelated lockfile change is still in the diff.

Recommendation: 💬 Optional Nits

  • [INFO] MODULE.bazel.lock is still changed in this pull request (usagesDigest and the wpt archive pin), although it is unrelated to shutdownDelayMs and the discussion says it was dropped. (MODULE.bazel.lock:408)
View full review

MODULE.bazel.lock is still in the diff (INFO)

The hermetic_launcher downgrade is gone. The file still changes the usagesDigest and moves the wpt archive from wpt-2e4dc257c to wpt-8ae17e423. That pin matches build/deps/gen/shared_deps.MODULE.bazel and build/deps/shared_deps.jsonc, so the change does no harm. It is still unrelated to this feature. The discussion says the lockfile was dropped from the branch, but it is still changed at this commit.

Code paths checked, no issues

  • run.rs: calling yield_now() and then now_or_never() catches a SIGTERM that arrives during startup, so the drain starts at once. On the first signal the /ready guard is dropped. The delay then races a second signal, and start_draining runs only after that race, so stopTimeoutMs is counted from the end of the delay.
  • entry.rs: if the signal handler cannot be installed, or the signal stream ends, the sigterm() stream waits forever instead of ending.
  • cli/config.rs: the one-hour limit is checked on the value from the config. The environment variable is read only when the field is 0, and the rest of the config is kept when the root is rewritten.
  • compose.rs: a deployment that sets shutdownDelayMs is refused, and a test covers this.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues. WORKERD_SHUTDOWN_DELAY_MS must be digits only, and the delay from either source is capped at one hour. Any invalid value stops the process at startup. Answering 503 on /ready does not change any access control.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


⚡ Performance: ✅ LGTM

The change touches only startup and shutdown: one yield_now() at startup, then one timer and one stream poll at shutdown. Nothing runs per request, and nothing grows without bound.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 1db663d5

Comment thread MODULE.bazel.lock Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from 1db663d to 96685e5 Compare November 23, 2026 22:55

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of ae40bb2f.

This review, of 96685e52

✅ Approved — No findings. The shutdown delay, the environment variable and the /ready handling work as described, tests cover them, and the unrelated lockfile change is gone.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues. WORKERD_SHUTDOWN_DELAY_MS must be digits only, and the delay is capped at one hour from either source. Any invalid value stops the process at startup. A deployment can't set the node's shutdown delay. The /ready change only makes the 503 start earlier.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

No issues found. MODULE.bazel.lock is no longer part of this pull request.

Code paths checked:

  • Config and environment variable: the config value is capped at one hour. The variable is read only when the field is 0. A blank or unset variable leaves the config as it is, and the rest of the config is kept.
  • Shutdown sequence: a SIGTERM during startup drains at once. With a delay of 0, draining starts directly after the signal. stopTimeoutMs starts counting when the delay ends.
  • Deployments: a deployment that sets shutdownDelayMs is refused, and a test covers this.

⚡ Performance: ✅ LGTM

The change runs only at startup and shutdown. Nothing runs per request.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 96685e52

@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from 96685e5 to ae40bb2 Compare November 23, 2026 23:38
@piscisaureus

Copy link
Copy Markdown
Contributor Author

With #8274 split up, WORKERD_CLUSTER_STOP_TIMEOUT_MS is now here after all (ae40bb2), so the delay and the stop timeout are set in the same place. Both variables also override the config when set, rather than fill a default, and #8841 does the same for the cluster key and bucket.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of d89ef7aa.

This review, of ae40bb2f

💬 Approved with comments — No blocking or warning findings. One INFO: a non-UTF-8 WORKERD_CLUSTER_STOP_TIMEOUT_MS is silently ignored, while a non-UTF-8 WORKERD_SHUTDOWN_DELAY_MS ends the process at startup.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

Behaviour is correct and well tested; one minor inconsistency in how the two environment variables are read.

Recommendation: 💬 Optional Nits

  • [INFO] A non-UTF-8 WORKERD_CLUSTER_STOP_TIMEOUT_MS is silently ignored, while a non-UTF-8 WORKERD_SHUTDOWN_DELAY_MS ends the process at startup. (src/workerd/server/cli/config.rs:159)
View full review

WORKERD_CLUSTER_STOP_TIMEOUT_MS that is not valid UTF-8 is ignored (src/workerd/server/cli/config.rs:159, src/workerd/server/cli/lib.rs:551)

with_cluster gets its environment through an Env closure built with std::env::var(name).ok(). A value that is set but not UTF-8 therefore reaches milliseconds as None, and the config's stopTimeoutMs stays in effect with no message. with_shutdown_delay_environment reads its variable as an OsString, so it rejects the same input with "not UTF-8 text." The docs say that for both variables any value other than a whole number of milliseconds ends the process.

Checked and fine:

  • A config without a cluster returns early, so no stop timeout is written into it.
  • The delay is capped at an hour from either source, and blank values count as unset.
  • A SIGTERM before serving drains at once, and a second SIGTERM cuts the delay short.
  • A deployment that sets shutdownDelayMs is refused, and a test covers that.

✅ All other checks passed

🔒 Security: ✅ LGTM

Both variables accept ASCII digits only, parse into u32 and are capped at an hour. A bad value fails at startup instead of falling back to a default. A configured delay above an hour is also rejected, and a deployment cannot set the node's shutdown delay.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


⚡ Performance: ✅ LGTM

These changes run only at startup and shutdown. Nothing runs per request.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: ae40bb2f

Comment thread src/workerd/server/cli/config.rs Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from ae40bb2 to d89ef7a Compare November 24, 2026 00:11

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 37c64fd2.

This review, of d89ef7aa

💬 Approved with comments — No blocking or warning findings. The earlier UTF-8 inconsistency is fixed; one INFO about a doc comment that describes the environment override wrongly.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

One doc comment describes the environment override inaccurately.

Recommendation: 💬 Optional Nits

  • [INFO] with_environment doc comment says the environment only fills defaults, but WORKERD_SHUTDOWN_DELAY_MS overrides a configured delay. (src/workerd/server/cli/lib.rs:515)
View full review

The doc comment on with_environment (src/workerd/server/cli/lib.rs:515-516) says it fills only the settings the config leaves at their defaults. with_shutdown_delay_environment (config.rs:194-198) takes the variable over the config's value whenever it is set.


✅ All other checks passed

🔒 Security: ✅ LGTM

Each variable accepts only a whole number of milliseconds, parsed into u32 and capped at an hour. A bad value ends the process at startup, and a non-UTF-8 cluster variable now does too. A deployment cannot set a node's shutdown delay.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

cluster_var (src/workerd/server/cli/lib.rs:531-540) treats an unset or empty variable as unset and ends the process on a non-UTF-8 value. with_cluster uses it for the stop timeout, key and bucket variables, so these variables now act the same way as WORKERD_SHUTDOWN_DELAY_MS.


⚡ Performance: ✅ LGTM

These changes run only at startup and shutdown.


Reviewed commit: d89ef7aa

Comment thread src/workerd/server/cli/lib.rs Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from d89ef7a to 37c64fd Compare November 24, 2026 00:26
A cloud load balancer in front of Kubernetes keeps sending new
connections to a terminating pod for up to about 20 s, because
endpoint removal propagates concurrently with SIGTERM. A node that
drains at once refuses those connections; today a preStop sleep is the
only way around that.

* `Config.shutdownDelayMs` makes the first SIGTERM (Ctrl-Break on
  Windows) log one line and keep serving as before for that long, then
  run the existing drain unchanged. Listeners keep accepting, and a
  cluster node keeps its lease, membership and Durable Objects
  meanwhile.
* A second SIGTERM during the delay starts the drain at once.
* A SIGTERM that arrives while the server starts drains it as soon as
  it is up, without the delay: it was never ready, so nothing sends it
  traffic.
* The default is 0, which drains at once, as before.
* WORKERD_SHUTDOWN_DELAY_MS overrides the field for `serve`, for
  wrangler.json users, who never see the Cap'n Proto config, and for
  deployments that adjust a baked config. A binary from `workerd
  compile` carries the field and takes the variable when it runs, as
  `serve` does; `compile` never embeds the environment.
* An empty variable counts as unset, leaving the config's delay, and
  trailing whitespace, such as a final newline, is trimmed. It must then hold digits only; a value
  that is not a whole number of milliseconds, or not UTF-8, ends the
  process at startup with an error that names it.
* A delay above 3600000 ms (an hour), from the variable or, when that
  is unset, the config, ends the process at startup too. The delay waits out load
  balancers, which takes seconds, so a larger value is a unit mistake
  that would leave a stopping node serving for hours, and a cluster
  node killed by its supervisor before it hands its objects off.
* A deployment that sets `shutdownDelayMs` is refused with an error
  that names the field, like the other node settings.
* `ClusterConfig.stopTimeoutMs` runs from the end of the delay, so the
  supervisor's grace period must cover both; the schema and
  `serve --help` say so.

`run::run` now takes the drain signals as a stream. Once the server
has started, it yields to the runtime once and then checks whether a
signal is pending. The yield matters: a start that never waits, such
as one evaluating a large worker, can return without tokio ever having
polled its drivers, so a signal from that time has not reached the
stream yet. tokio's current_thread scheduler wakes a yielded task only
after polling its drivers, signal handling included. The listeners
then race `drain_due`, which waits for the first signal and the delay
or a second signal, so a listener failing during the delay is still
fatal.

server-test.c++ drives the delay with the mock timer, configured
through the field: the server serves through it and drains after it,
a second signal cuts it short, and a signal sent before the server
starts drains it without the timer advancing. cluster-test.mjs sends
a real SIGTERM to a server whose worker keeps it starting for seconds
and checks that it exits at once, which fails without the yield. The
CLI's config tests cover the variable unset or blank, valid with and
without trailing whitespace, overriding the config, unusable even
over a configured delay, not UTF-8, and the cap on the config field; compose-test.rs covers the
deployment.
During the shutdown delay a cluster node serves as before, but nothing
should send it new traffic. Readiness is all that tells a Service or a
load balancer health check so.

* `GET /ready` on the cluster port answers 503 from the first SIGTERM
  on, through the delay, where it answered 200 until the drain began.
  Only readiness probes read it: peers route by lease records and
  draining announcements, so they keep reaching the node's Durable
  Objects meanwhile. Without a delay the drain starts at once, which
  already answered 503, so nothing changes there.
* `ClusterNode::serving` and its guard become `ready` / `Ready`: the
  flag now means "takes traffic", not only "listeners are served".
  `drain_due` drops the guard on the first signal.
* `Config.shutdownDelayMs`, `ClusterConfig` and `serve --help` say
  that `/ready` answers 503 during the delay.

cluster-test.mjs checks, with WORKERD_SHUTDOWN_DELAY_MS, that a node
with a 6 s delay answers 503 at once and keeps its lease and its DO,
which runs there whether it is asked there or on the peer, then hands
the DO off and leaves once the delay is over. Requests stop 2 s before
the delay ends, and only those answered within it must have run on the
stopping node, so a slow request cannot meet the handoff and fail the
test. A node given a 600 s delay is still a member 2 s after the first
SIGTERM, well after a drain would have left, and stops within the stop
timeout of the second. Polling `/ready` is bounded by a per-request
timeout and a deadline, so a missed signal fails the test.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 8e3a148c.

This review, of 37c64fd2

💬 Approved with comments — No blocking findings. One WARNING: the new non-UTF-8 error path in cluster_var writes the WORKERD_CLUSTER_KEY value to stderr, so most of the key's characters can end up in pod logs.

Findings: 1 warning (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8427.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Security] `cluster_var` prints the raw value of a non-UTF-8 variable, including the secret `WORKERD_CLUSTER_KEY`, to stderr.
- **File:** src/workerd/server/cli/lib.rs:536

🔒 Security: ⚠️ 1 warning

The non-UTF-8 error in cluster_var writes the cluster key's value to stderr

Recommendation: 📝 Minor Comments

  • [WARNING] cluster_var prints the raw value of a non-UTF-8 variable, including the secret WORKERD_CLUSTER_KEY, to stderr. (src/workerd/server/cli/lib.rs:536)
View full review

cluster_var (src/workerd/server/cli/lib.rs:531-540) reads WORKERD_CLUSTER_KEY as well as the bucket and stop-timeout variables. When a value is not UTF-8, line 536 prints {name}={value} using to_string_lossy(). For the cluster key, every byte that is valid UTF-8 shows up verbatim in stderr, and stderr usually lands in container logs. A key pasted in as raw random bytes is the case most likely to hit this path.

The key-file path already handles this safely: read_key_file (config.rs:393) reports only that the file is not UTF-8, not its contents.


✅ All other checks passed

📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

cluster_var (src/workerd/server/cli/lib.rs:531-540) treats a variable that is unset or empty as unset. A value that is not UTF-8 ends the process. with_cluster passes it only under --experimental, as it did the old closure. with_environment runs for serve and for a compiled binary, never for compile, so the environment is never embedded in a compiled config.


⚡ Performance: ✅ LGTM

These changes run only at startup and shutdown.


📝 Documentation: ✅ LGTM

The with_environment doc comment now says the environment overrides the config, which matches with_shutdown_delay_environment.


Reviewed commit: 37c64fd2

Comment thread src/workerd/server/cli/lib.rs Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from 37c64fd to 8e3a148 Compare November 24, 2026 00:30

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 3baa7a37.

This review, of 8e3a148c

💬 Approved with comments — Nothing blocks merging. One WARNING remains from the last review: if WORKERD_CLUSTER_KEY holds bytes that are not valid UTF-8, cluster_var prints most of the key to stderr, which usually ends up in pod logs.

Findings: 1 warning

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8427.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Security] `cluster_var` prints the value of a non-UTF-8 variable, including the secret `WORKERD_CLUSTER_KEY`, to stderr.
- **File:** src/workerd/server/cli/lib.rs:536

🔒 Security: ⚠️ 1 warning

cluster_var still prints the raw value of a non-UTF-8 WORKERD_CLUSTER_KEY to stderr.

Recommendation: 📝 Minor Comments

  • [WARNING] cluster_var prints the value of a non-UTF-8 variable, including the secret WORKERD_CLUSTER_KEY, to stderr. (src/workerd/server/cli/lib.rs:536)
View full review
  • src/workerd/server/cli/lib.rs:535-536: cluster_var reads WORKERD_CLUSTER_KEY, the bucket variable and the stop-timeout variable. When a value is not valid UTF-8, it prints {name}={value} using to_string_lossy(). For the cluster key, every byte that is valid UTF-8 appears as-is in stderr, and stderr usually goes to container logs. The key-file path already avoids this: read_key_file says only that the file is not UTF-8 text. Printing just the variable name fixes it for all three variables.

✅ All other checks passed

📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

No new issues since the last review. The stderr problem in cluster_var is covered in the security section.


⚡ Performance: ✅ LGTM

These changes only run at startup and shutdown.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 8e3a148c

@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from 8e3a148 to 3baa7a3 Compare November 24, 2026 00:56

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 9369cdc1.

This review, of 3baa7a37

💬 Approved with comments — The earlier WARNING about the cluster key reaching stderr is fixed, and no new defects were found. One INFO remains: the lockfile change is unrelated to the feature.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

One INFO: MODULE.bazel.lock is back in the diff (it matches the declared capnproto pin).

Recommendation: 💬 Optional Nits

  • [INFO] MODULE.bazel.lock is back in the diff (capnproto pin and usagesDigest), unrelated to the shutdown delay. (MODULE.bazel.lock:436)
View full review

The two files changed since the last review, cli/lib.rs and MODULE.bazel.lock, were re-checked.

  • with_environment runs in the compiled-binary path and in serve, before with_cluster, and not in compile. The environment therefore never ends up in compiled output.
  • with_cluster gets cluster_var as its Env. Its stop-timeout read gets the same non-UTF-8 handling as the other variables.
  • MODULE.bazel.lock changes the capnproto archive pin to ee05f52. That matches build/deps/gen/deps.MODULE.bazel:28-34, but the change is unrelated to this feature.

✅ All other checks passed

🔒 Security: ✅ LGTM

The NotUnicode(_) arm of cluster_var (src/workerd/server/cli/lib.rs:536-539) discards the value. It prints only {name}: not UTF-8 text. and exits. None of the cluster key, bucket or stop-timeout variables can leak a value this way. The values of the shutdown-delay and stop-timeout variables are bounded whole-number milliseconds and are not secret.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


⚡ Performance: ✅ LGTM

These changes only run at startup and shutdown.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 3baa7a37

Comment thread MODULE.bazel.lock Resolved
A supervisor's grace period must cover the shutdown delay and the
cluster's stop timeout together. The delay can be set in the
environment, but a wrangler.json project, which has no Cap'n Proto
config, had no way to set the stop timeout. Both timings now come from
the same place.

* `serve`, and a binary from `workerd compile`, take
  `ClusterConfig.stopTimeoutMs` from WORKERD_CLUSTER_STOP_TIMEOUT_MS
  when it is set, over the config's, as WORKERD_SHUTDOWN_DELAY_MS
  overrides `shutdownDelayMs`. That includes a cluster joined with
  `--cluster-addr`, since the variable is read in the same pass
  (`Config::with_cluster`). A config without a cluster is unchanged.
* The variable is validated as WORKERD_SHUTDOWN_DELAY_MS is: blank
  counts as unset, trailing whitespace is trimmed, and anything but a
  whole number of milliseconds up to 3600000 (an hour) ends the process
  at startup, naming the variable. The two share one parser.
* A cluster variable that is not UTF-8, this one or the key and bucket
  variables, now ends the process with an error that names it, as a
  non-UTF-8 WORKERD_SHUTDOWN_DELAY_MS does. It used to count as unset.
* workerd.capnp, `serve --help` and AGENTS.md describe the variable.

The CLI's config tests cover the variable overriding a default and a
configured timeout, unset or blank, unusable, and applied to a joined
cluster but not to a config without one. A non-UTF-8 value, which the
tests cannot set, was checked by hand.
@piscisaureus
piscisaureus force-pushed the piscisaureus/shutdown-delay branch from 3baa7a3 to 9369cdc Compare November 24, 2026 01:24

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues remain open. The unrelated lockfile change is gone, and the shutdown-delay, readiness and environment-variable handling look correct.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

For a non-UTF-8 value, cluster_var prints only the variable's name, never the value, so WORKERD_CLUSTER_KEY cannot reach stderr.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

MODULE.bazel.lock is no longer in the diff.

What I checked:

  • Signals: sigterm() in entry.rs and the drain check in run.rs behave as their comments describe in three cases: a signal before the server serves, a second signal during the delay, and a handler that fails to install.
  • Readiness: cluster /ready answers 503 from the first SIGTERM on.
  • Stop timeout: stopTimeoutMs runs from the end of the delay.
  • Environment: with_environment runs only on the serve and compiled-binary paths, never in compile.
  • Deployments: compose.rs refuses a deployment that sets shutdownDelayMs.

⚡ Performance: ✅ LGTM

These changes only run at startup and shutdown.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 9369cdc1

@piscisaureus
piscisaureus merged commit 98db500 into main Nov 24, 2026
50 of 52 checks passed
@piscisaureus
piscisaureus deleted the piscisaureus/shutdown-delay branch November 24, 2026 02:04
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.

4 participants