← Back to Pull requests

Fence a cluster node whose event loop makes no progress #8637

Closed
unknown closed this pull request without merging on Nov 20, 2026 23:54 UTC
View on GitHub
unknown unknown commented on Nov 18, 2026 23:57 UTC
Author

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 CPU
limit for cluster workers (NullIsolateLimitEnforcer), so nothing
interrupts 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's
other 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::ProgressProbe event moves
to 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. The
event 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 LocalSet advances the same
count once per lease-renewal interval.
kj_rs_tokio::watch_progress() provides a thread-safe view of the
count.
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 SIGSTOP or a VM pause.
Watching starts only at ClusterNode::serving(). Linking the workers
after 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::fence path as a lost lease. It
therefore 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() or yieldUntilQueueEmpty() event, which KJ queues
where the probe cannot follow;
a yieldUntilWouldSleep() waiter, which KJ arms only after the
port'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, would
close these gaps and remove the need for the probe. That is a
follow-up. progress.rs documents 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() nor WaitScope::poll(). They also check
that 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 Watch command.
A startup test checks that a long synchronous turn after the node
joins does not trip the watchdog.
cluster-test.mjs adds a /wedge endpoint whose worker loops
forever. Under the new shortLease config (a 6 s threshold), the
node 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-test passed. All 200 shard runs of cluster-test also
passed. These runs predate the rebase onto main; on main, the same
tests pass again.

  • Clang-tidy and clippy are clean.
  • Lines changed

    | Kind | Added | Removed |
    | -------------- | ----: | ------: |
    | Implementation | 434 | 21 |
    | Tests | 571 | 9 |
    | Documentation | 7 | 2 |

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: piscisaureus Review duration: 2877m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%