Repository navigation
Fence a cluster node whose event loop makes no progress - #8637
Conversation
There was a problem hiding this comment.
Superseded by the review of
a15ea2b9.
This review, of 0d0fa671
💬 Approved with comments — No blocking or warning-level issues; one suggestion (log when the watchdog cannot be armed) and two informational notes on test timing and documentation wording.
Findings: 2 info · 1 suggestion (3 are inline comments 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 · 1 suggestion
Sound design; the watchdog fails to arm silently, and one test has tight timing slack.
Recommendation: 💬 Optional Nits
- [INFO] 2 ms timing slack in the keeper fence-timing assertion may be flaky under load (
src/workerd/server/cluster/keeper-test.rs:364) - [SUGGESTION] If the watchdog cannot be armed,
serving()runs the node unwatched and logs nothing (src/workerd/server/server/cluster.rs:497)
View full review
src/workerd/server/server/cluster.rs:497: ifkj_rs_tokio::watch_progress()returnsNone, the node runs without a watchdog and logs nothing. That happens with no port orLocalSet, or whenstart_progress_probe()fails, and that error is dropped by.ok()?.watchedis still set totrue, so it never retries.src/workerd/server/cluster/keeper-test.rs:364(and:396): the assertion allows only 2 ms between the watchdog'ssince_msand the test'slast_msstamp. A descheduled thread on a loaded CI machine could fail it.
📝 Documentation: 💬 1 info
The new AGENTS.md notes are worded more broadly than the documented gaps.
Recommendation: 💬 Optional Nits
- [INFO] AGENTS.md note says progress is counted "busy or asleep" without the documented busy-loop gaps (
src/rust/cxx/AGENTS.md:41)
View full review
src/rust/cxx/AGENTS.md:40-41: the note says the counter tracks progress "busy or asleep", butprogress.rsdocuments busy stretches it does not count.
✅ All other checks passed
🔒 Security: ✅ LGTM
The watchdog adds no new untrusted input, authentication change or secret handling. Fencing goes through the existing fail-closed path, and the /wedge endpoint exists only in test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
Reviewed commit: 0d0fa671
0d0fa67 to
a15ea2b
Compare
There was a problem hiding this comment.
Superseded by the review of
c4c4571d.
This review, of a15ea2b9
✅ Approved — No issues found. The suggestion and two informational notes from the previous review are all fixed at this commit.
Not opened by the correctness review: 1 changed file it was meant to read
src/rust/cxx/kj-rs-tokio/lib.rs
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
The watchdog adds no untrusted input, no authentication change and no secret handling. Fencing goes through the existing fail-closed Armed::fence path. The /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
Both earlier notes are fixed:
serving()now logs atracing::warn!with theWatchErrorreason (NoPortorProbe(String)) and keeps serving unwatched.- The keeper fence-timing assertions now allow a quarter of
stall_ms.
Watchdog::observe starts timing when it first sees a count value and fences only after that count has stood still for stall_ms. The keeper checks it after the lease step, so a whole-process pause is still reported as a lost lease.
⚡ Performance: ✅ LGTM
The probe adds one KJ turn per pass through a busy run queue, and only in cluster mode. The lease thread reads the counter once per round, which costs very little.
📝 Documentation: ✅ LGTM
Both AGENTS.md notes now say the count misses the busy stretches that progress.rs lists.
Reviewed commit: a15ea2b9
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8637 +/- ##
==========================================
- Coverage 67.34% 67.30% -0.04%
==========================================
Files 836 838 +2
Lines 213706 213835 +129
Branches 32485 32490 +5
==========================================
+ Hits 143922 143931 +9
- Misses 50687 50809 +122
+ Partials 19097 19095 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ce76f07 to
1728f15
Compare
a15ea2b to
c4c4571
Compare
There was a problem hiding this comment.
Superseded by the review of
a216de7a.
This review, of c4c4571d
✅ Approved — No findings. The code is unchanged since the previous approval, and kj-rs-tokio/lib.rs, which that review did not open, only adds re-exports and a module declaration.
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 defects found. Fencing uses the existing fail-closed Armed::fence path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
src/rust/cxx/kj-rs-tokio/lib.rs only adds the mod progress; declaration, a doc line and re-exports of watch_progress, Progress and WatchError. current_local_set in port.rs is now pub, but its module is private and the function is not re-exported, so the crate's public API stays the same.
The progress probe code checks out:
- Destruction order:
progressProbeis declared afterloopandrustPort, so the probe is destroyed before both. - Probe start: starting the probe more than once is safe. It is created once and only re-armed after that.
- Idle beat task: it stops once the last
Progresshandle is dropped.
⚡ Performance: ✅ LGTM
No performance issues found. The probe adds one KJ turn per pass through a busy run queue, and only in cluster mode.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: c4c4571d
1728f15 to
088fe06
Compare
c4c4571 to
a216de7
Compare
There was a problem hiding this comment.
Superseded by the review of
b9cd8ec1.
This review, of a216de7a
✅ Approved — No findings. The changes since the last approval to the keeper, serving() and the test config check out, and the earlier points raised in discussion are fixed.
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. Fencing uses the existing fail-closed path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No issues found.
keeper.rs:Watchgoes through the same command channel asMaintain. The watchdog checks progress after the lease step, and its reason goes throughArmed::fence.server/cluster.rs:serving()starts the watchdog only once, guarded bywatched.replace(true). Ifwatch_progressfails, it logs a warning and serves on unwatched.cluster-test.wd-test:shortLeasehasunsafeTimeDivisor = 10, which matchesSHORT_LEASE_STALL_MS.
⚡ Performance: ✅ LGTM
No performance issues found. The keeper reads one atomic counter per lease round, and in cluster mode the probe adds one KJ turn per pass through a busy run queue.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: a216de7a
jasnell
left a comment
There was a problem hiding this comment.
LGTM but I'm less familiar with the kj-rs-tokio details. Might be good for @guybedford to also look
a216de7 to
b9cd8ec
Compare
There was a problem hiding this comment.
Superseded by the review of
541b19aa.
This review, of b9cd8ec1
✅ Approved — No findings. The changes since the last approval to the keeper, serving() and the test config check out, and the earlier points raised in discussion are fixed.
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. Fencing uses the existing fail-closed path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No issues found.
keeper.rs:Watchgoes through the same command channel asMaintain. The watchdog checks progress after the lease step, and its reason goes throughArmed::fence.server/cluster.rs:serving()starts the watchdog only once, guarded bywatched.replace(true). Ifwatch_progressfails, it logs a warning and serves on unwatched.cluster-test.wd-test:shortLeasehasunsafeTimeDivisor = 10, which matchesSHORT_LEASE_STALL_MS.
⚡ Performance: ✅ LGTM
No performance issues found. The keeper reads one atomic counter per lease round, and in cluster mode the probe adds one KJ turn per pass through a busy run queue.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: b9cd8ec1 — same changes as a216de7a, so this is that commit's review (no new review was run; line numbers are as of a216de7a)
b9cd8ec to
541b19a
Compare
There was a problem hiding this comment.
Superseded by the review of
575d1750.
This review, of 541b19aa
✅ Approved — No findings. The changes since the last approval to the keeper, serving() and the test config check out, and the earlier points raised in discussion are fixed.
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. Fencing uses the existing fail-closed path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No issues found.
keeper.rs:Watchgoes through the same command channel asMaintain. The watchdog checks progress after the lease step, and its reason goes throughArmed::fence.server/cluster.rs:serving()starts the watchdog only once, guarded bywatched.replace(true). Ifwatch_progressfails, it logs a warning and serves on unwatched.cluster-test.wd-test:shortLeasehasunsafeTimeDivisor = 10, which matchesSHORT_LEASE_STALL_MS.
⚡ Performance: ✅ LGTM
No performance issues found. The keeper reads one atomic counter per lease round, and in cluster mode the probe adds one KJ turn per pass through a busy run queue.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 541b19aa — same changes as a216de7a, so this is that commit's review (no new review was run; line numbers are as of a216de7a)
541b19a to
575d175
Compare
There was a problem hiding this comment.
Superseded by the review of
6ea93fe3.
This review, of 575d1750
✅ Approved — No findings. The changes since the last approval to the keeper, serving() and the test config check out, and the earlier points raised in discussion are fixed.
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. Fencing uses the existing fail-closed path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No issues found.
keeper.rs:Watchgoes through the same command channel asMaintain. The watchdog checks progress after the lease step, and its reason goes throughArmed::fence.server/cluster.rs:serving()starts the watchdog only once, guarded bywatched.replace(true). Ifwatch_progressfails, it logs a warning and serves on unwatched.cluster-test.wd-test:shortLeasehasunsafeTimeDivisor = 10, which matchesSHORT_LEASE_STALL_MS.
⚡ Performance: ✅ LGTM
No performance issues found. The keeper reads one atomic counter per lease round, and in cluster mode the probe adds one KJ turn per pass through a busy run queue.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 575d1750 — same changes as a216de7a, so this is that commit's review (no new review was run; line numbers are as of a216de7a)
A cluster node keeps its lease on a thread of its own, so a turn of
its event loop of any length keeps it. A loop that is stuck for good
(a deadlock, native code that does not return, a turn that never
ends) therefore kept the lease, and its Durable Objects unreachable,
for as long as the process lived.
Once the node serves, the lease thread now watches the loop. A loop
that makes no progress fences the node through the same path and
fatal line as a lease that cannot be kept:
* `kj_rs_tokio::watch_progress()` gives a count that the loop advances
while it makes progress, readable from any thread. A busy KJ loop
polls its port only once its run queue is empty, so between its
turns no tokio task runs, no KJ timer fires (`TimerImpl` fires only
after the port's `wait()` or `poll()`) and no `kj::Executor` ping is
taken in (drained only after the port wakes). The count's busy half
is therefore a KJ event, the port's progress probe: it counts and
re-arms itself breadth-first, so it fires once per pass through the
run queue, and leaves the queue once nothing but itself is left, so
that the loop still sleeps and runs its `evalLast()` events. The
port re-arms it after every `wait()`, and after a `poll()` that
leaves events to turn. While the loop sleeps, a task on its
`LocalSet` counts once a beat.
* The count stands still while one event holds the thread, and also
through three kinds of busy stretch that begin while the probe is
out of the queue:
* one that an `evalLast()` / `yieldUntilQueueEmpty()` event starts:
`armLast()` queues each in front of the ones before, so the probe
cannot follow them;
* one that a `yieldUntilWouldSleep()` waiter starts: KJ arms the
waiter after the port's `poll()` returns, and only if that poll
left no event;
* one that a cross-thread event starts when KJ takes it in after
the port's `poll()` rather than its `wait()`. Arming the probe for
the wake would let a stale wake, whose event KJ cancelled without
consuming it, keep `WaitScope::poll()` from returning and starve
would-sleep waiters.
Such a stretch is counted again once the run queue empties. Only
work that is ready without the port keeps it from emptying, and one
that lasts a whole `stall_ms` fences the node. Closing all three
needs a per-turn hook in KJ's `EventLoop::turn()`.
* `cluster_logic::watchdog` decides: a count that stands still for
`Timing::stall_ms` fences the node. The keeper reads it once a
round, after the round's lease step, so that a process stopped as a
whole reports its lapsed lease. Its reason ends the rounds as a
lapsed lease does, through `Armed::fence`, while the lease holds.
* The watching starts with `ClusterNode::serving`, which sends the
count to the lease thread (`Cluster::watch`). Linking, after `join`,
runs in turns as long as itself, and the lease thread keeps the
lease through it as before. A loop that cannot be watched
(`watch_progress()` answers why) serves on unwatched, with a
warning that says so.
* `stall_ms` is three leases, 60 s, divided by `unsafeTimeDivisor` as
the other times are. That is twice the turn of one and a half
leases that the long-turn test rides out. The beat is the renewal
interval.
* The fatal line now reads "leaving the cluster: {reason}", where a
lease's reasons begin "its lease cannot be kept: " and the
watchdog's reads "its event loop made no progress for N ms".
The probe costs a KJ turn per pass through a busy run queue and one
per wake of the loop, in cluster mode only. A chain of events that
arm each other depth-first runs ahead of it, as it runs ahead of
every other event, and counts as one event until it ends.
The cluster test's Worker gains a `/wedge` endpoint that loops
forever, and a test that its node exits with status 1 and the reason
on stderr, under a `shortLease` config with a tenth of the default
times. The long-turn test now aims at one and a half leases, at most
four times its last guess, rather than doubling past a lease.
575d175 to
6ea93fe
Compare
There was a problem hiding this comment.
✅ Approved — No findings. The changes since the last approval to the keeper, serving() and the test config check out, and the earlier points raised in discussion are fixed.
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. Fencing uses the existing fail-closed path, and the /wedge endpoint exists only in the test config.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No issues found.
keeper.rs:Watchgoes through the same command channel asMaintain. The watchdog checks progress after the lease step, and its reason goes throughArmed::fence.server/cluster.rs:serving()starts the watchdog only once, guarded bywatched.replace(true). Ifwatch_progressfails, it logs a warning and serves on unwatched.cluster-test.wd-test:shortLeasehasunsafeTimeDivisor = 10, which matchesSHORT_LEASE_STALL_MS.
⚡ Performance: ✅ LGTM
No performance issues found. The keeper reads one atomic counter per lease round, and in cluster mode the probe adds one KJ turn per pass through a busy run queue.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 6ea93fe3 — same changes as a216de7a, so this is that commit's review (no new review was run; line numbers are as of a216de7a)
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
logs
leaving the cluster: its event loop made no progress for N ms.Every fence now logs
leaving the cluster: {reason}.unsafeTimeDivisor, like the cluster'sother times.
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:
TokioEventPort::ProgressProbeevent movesto the back of the KJ queue each time it runs. It counts each trip
through the queue.
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.
LocalSetadvances the samecount once per lease-renewal interval.
kj_rs_tokio::watch_progress()provides a thread-safe view of thecount.
The lease thread checks the count after each lease step. The decision
logic lives in
cluster_logic::watchdog:the whole process pauses, such as during
SIGSTOPor a VM pause.ClusterNode::serving(). Linking the workersafter the node joins is one long synchronous turn. It must not look
like a stall.
and serves unwatched.
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:
evalLast()oryieldUntilQueueEmpty()event, which KJ queueswhere the probe cannot follow;
yieldUntilWouldSleep()waiter, which KJ arms only after theport's poll found nothing to do;
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
count.
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.
that a stuck loop fences while the lease still holds, and that the
rounds watch only after the
Watchcommand.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 threshold.
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