Fence a cluster node whose event loop makes no progress #8637
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8637"
general
weight: 0.35
piscisaureus
A cluster node keeps its Durable Objects while it holds a lease in the
bucket. Peers take over those objects when the lease expires. This
allows the cluster to survive a node that crashes, hangs, or loses its
network.
Since #8631, a separate thread renews the node's lease. A busy event
loop no longer causes the node to lose its lease. A long synchronous
turn, a slow SQLite query, or a burst of storage work no longer fences
a healthy node.
However, the lease thread also renews the lease when the event loop is
stuck for good. This can happen when a Durable Object runs
while (true) {}or a native call never returns. workerd has no CPUlimit for cluster workers (
NullIsolateLimitEnforcer), so nothinginterrupts such a loop. The node keeps its lease and every object it
owns forever, but it cannot answer requests for any of them.
This PR adds a watchdog for that case. The lease thread watches a
counter that tracks event-loop progress. If the counter does not
change for three leases (60 s by default), the node leaves the cluster
as if it had lost its lease. It exits with status 1 while the lease
still holds. The supervisor restarts it, and peers take over its
objects when the lease expires.
What operators see
A node exits with status 1 when its event loop is stuck for 60 s. It
logs
leaving the cluster: its event loop made no progress for N ms.Every fence now logs
leaving the cluster: {reason}.The threshold scales with
unsafeTimeDivisor, like the cluster'sother times.
Nothing changes outside cluster mode.
How progress is counted
A plain heartbeat does not work. This includes a timer, a tokio
interval, or a ping through the executor. KJ polls its timers and event
port only when its run queue is empty. A healthy loop may run many
short turns without ever emptying the queue, such as while serving a
fast upload. The heartbeat would not fire, so the watchdog could
mistake useful work for a stuck loop.
The counter advances in two ways:
While the loop is busy, a
TokioEventPort::ProgressProbeevent movesto the back of the KJ queue each time it runs. It counts each trip
through the queue.
When the probe is the only event left, it leaves the queue. This lets
the loop sleep and allows
evalLast()events to run as usual. Theevent port puts the probe back after every wait and after a poll that
leaves events to run.
While the loop is idle, a task on its
LocalSetadvances the samecount once per lease-renewal interval.
kj_rs_tokio::watch_progress()provides a thread-safe view of thecount.
A depth-first chain counts as one event until it finishes.
The lease thread checks the count after each lease step. The decision
logic lives in
cluster_logic::watchdog:Checking after the lease step preserves lost-lease reporting when
the whole process pauses, such as during
SIGSTOPor a VM pause.Watching starts only at
ClusterNode::serving(). Linking the workersafter the node joins is one long synchronous turn. It must not look
like a stall.
If the watchdog cannot start, the node logs a warning with the reason
and serves unwatched.
The fence uses the same
Armed::fencepath as a lost lease. Ittherefore happens once and while the lease still holds.
Known gaps
Three cases let a busy loop make progress without moving the count.
In each, a busy stretch begins while the probe is out of the queue,
and something other than the port's wait starts it:
an
evalLast()oryieldUntilQueueEmpty()event, which KJ queueswhere the probe cannot follow;
a
yieldUntilWouldSleep()waiter, which KJ arms only after theport's poll found nothing to do;
an event from another thread, which KJ takes in only after the port's
poll returns. Re-arming the probe on every such wake is not safe. A
cancelled cross-thread call leaves its wake behind, so the probe
would keep
WaitScope::poll()from returning.The count moves again when the run queue empties. The watchdog fences
only if the queue stays busy for the full threshold. Such a stretch
also accepts no new connections, timers, or cross-thread events, so
the node is unresponsive during it anyway.
A per-turn counter in KJ's
EventLoop::turn(), in capnproto, wouldclose these gaps and remove the need for the probe. That is a
follow-up.
progress.rsdocuments the gaps.Cost
In cluster mode, the probe adds one KJ turn per pass through a busy run
queue and one each time the loop wakes.
Tests
Unit tests cover the watchdog's decisions and the Rust handle and
count.
C++ event-port tests check that the count advances in a busy loop
that never sleeps. They check that the probe lets the loop sleep and
delays neither
evalLast()norWaitScope::poll(). They also checkthat the count stands still while one event holds the thread and
cover the three gaps above.
Keeper tests check that a loop that makes progress keeps its lease,
that a stuck loop fences while the lease still holds, and that the
rounds watch only after the
Watchcommand.A startup test checks that a long synchronous turn after the node
joins does not trip the watchdog.
cluster-test.mjsadds a/wedgeendpoint whose worker loopsforever. Under the new
shortLeaseconfig (a 6 s threshold), thenode exits with status 1 after about 6.6 s and writes the watchdog's
reason to stderr.
The long-turn integration test now runs for up to 1.5 leases, below
the threshold.
Forty uncached runs each of the kj-rs-tokio tests, the event port
tests, the cluster Rust and logic tests, the server tests, and
cluster-cron-testpassed. All 200 shard runs ofcluster-testalsopassed. These runs predate the rebase onto main; on main, the same
tests pass again.
Lines changed
| Kind | Added | Removed |
| -------------- | ----: | ------: |
| Implementation | 434 | 21 |
| Tests | 571 | 9 |
| Documentation | 7 | 2 |