Repository navigation
Close peer sessions as moved when a cluster node stops - #8565
Conversation
There was a problem hiding this comment.
Superseded by the review of
1eedb9f0.
This review, of 7e1e6d91
💬 Approved with comments — No correctness or security defects found. The change also narrows the cluster port to clustered-actor lookups. Two INFO notes cover shutdown-phase behavior.
Findings: 2 info (2 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: 💬 2 info
No correctness bugs. Two minor shutdown-behavior notes.
Recommendation: 💬 Optional Nits
- [INFO] During stop, debug-port
getActorrequests are refused asmoved, a peer-routing error that debug clients won't expect. (src/workerd/server/server/cluster.rs:550) - [INFO] After the Abort, the drain loop waits until the peer closes its side. (
src/workerd/server/factory/worker-factory-rpc.c++:312)
View full review
- Debug-port requests answered
movedduring stop.resolve_actorcallspeer_channel(.., None), which goes throughpeer_request. Oncerefusing_peersis set, a debug client gets a peer-routingmovederror. The doc comment says this is intended, but the description says--debug-portis unchanged. This only happens in the final shutdown phase. - Post-Abort drain waits for the peer to close. After the Abort is written,
factory_accept_cluster_peerreads until EOF. A peer that never closes holds the session open until theconnectionsdeadline.
✅ All other checks passed
🔒 Security: ✅ LGTM
This change narrows the attack surface. The cluster peer port now serves only getActor, for clustered namespaces, and no longer exposes getEntrypoint to peers. The 403 authentication check in PeerConnection::connect is unchanged, and the new 503 refusal applies only after it.
📖 AGENTS.md: ✅ LGTM
No issues.
⚡ Performance: ✅ LGTM
The drain paths run once per stop, so they add no meaningful overhead.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 7e1e6d91
c180a34 to
1c6fd5c
Compare
7e1e6d9 to
1eedb9f
Compare
There was a problem hiding this comment.
Superseded by the review of
c88c6b3d.
This review, of 1eedb9f0
💬 Approved with comments — No correctness or security defects found at this commit. The two INFO notes on shutdown-phase behavior from the earlier review still apply.
Findings: 2 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 2 info
No correctness defects. Two INFO notes on shutdown-phase behavior still apply.
Recommendation: 💬 Optional Nits
- [INFO] During stop, debug-port
getActorrequests are refused with the peer-routingmovederror. (src/workerd/server/server/cluster.rs:549) - [INFO] After the Abort, the drain loop waits until the peer closes its side. (
src/workerd/server/factory/worker-factory-rpc.c++:311)
View full review
- Debug-port
getActoransweredmovedduring stop.peer_request(None)returnsmoved("this instance is stopping")oncerefusing_peersis set. The debug port reaches that call throughpeer_channel(id, None). The doc comment says this is intended, but the description says--debug-portis unchanged. It only happens in the final stop phase. - Post-Abort drain waits for the peer's EOF. After it writes the Abort and calls
shutdownWrite(),factory_accept_cluster_peerkeeps reading until the peer closes its side. A peer that ignores the Abort keepsstop_peer_sessionswaiting until theconnectionsdeadline. That deadline bounds the wait, so this delays stop but cannot hang it.
✅ All other checks passed
🔒 Security: ✅ LGTM
This change narrows the attack surface. The cluster peer port now serves only clustered-actor getActor lookups. The authentication check in PeerConnection::connect is unchanged, and the new 503 refusal applies only after it.
📖 AGENTS.md: ✅ LGTM
No issues.
⚡ Performance: ✅ LGTM
The drain paths run once per stop, so they add no meaningful overhead.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 1eedb9f0
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8565 +/- ##
==========================================
- Coverage 66.72% 66.57% -0.16%
==========================================
Files 797 797
Lines 204156 204630 +474
Branches 31724 31734 +10
==========================================
+ Hits 136231 136232 +1
- Misses 49329 49797 +468
- Partials 18596 18601 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
1c6fd5c to
b1c6ad6
Compare
1eedb9f to
c88c6b3
Compare
There was a problem hiding this comment.
Superseded by the review of
7dda9f2c.
This review, of c88c6b3d
✅ Approved — Both earlier notes on stop-time behaviour are fixed, and no new correctness, security or performance issues were found in the changed code.
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
resolve_peer_actorrejects namespaces that have no cluster, and the peer port does not implementgetEntrypoint.- The new 503 refusal in
PeerConnection::connectcomes after the existing key check. - The Abort message is built only from an exception this process creates, so it carries no data from the peer.
📖 AGENTS.md: ✅ LGTM
No issues.
📋 Code Quality: ✅ LGTM
Both earlier notes are fixed at this commit:
peer_requestrefuses only peer sessions. Debug-port calls are served and counted.- The read-until-close after the Abort is capped at 1 s by
PEER_CLOSE_TIMEOUT.
The refusal paths in actor.rs and cluster.rs happen before any work starts, so resending a refused call is safe.
⚡ Performance: ✅ LGTM
The new drain logic runs only during stop and is bounded by the 1 s close timeout and the connections deadline. Per-request counting costs an Rc increment and decrement.
📝 Documentation: ✅ LGTM
The updated ClusterConfig comment in workerd.capnp describes the new order of the peer drain.
Reviewed commit: c88c6b3d
b1c6ad6 to
58fc388
Compare
c88c6b3 to
7dda9f2
Compare
There was a problem hiding this comment.
Superseded by the review of
80d71be0.
This review, of 7dda9f2c
💬 Approved with comments — No correctness, security or performance problems in the changed code. One INFO: in the late part of a stop, debug-port requests can get a moved error from the handoff branch in actor.rs.
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
Earlier notes stay fixed; one minor inconsistency for debug-port requests late in a stop.
Recommendation: 💬 Optional Nits
- [INFO] Debug-port requests that wait on a handoff get
movedonce thein_flightdeadline has passed, even thoughpeer_requestnow spares them during stop. (src/workerd/server/server/actor.rs:971)
View full review
Both earlier notes are still fixed: peer_request refuses only peer sessions, and the wait for the peer to close after the Abort is bounded by PEER_CLOSE_TIMEOUT. closing resolves only after refusing_peers is set and the session's count reaches zero, so nothing new can be counted on a session after it closes.
One remaining inconsistency: the handoff branch at actor.rs:971-977 checks remote, which is also true for debug-port requests (peer_channel(.., None)). Once the in_flight deadline passes, those requests get moved instead of being forwarded. This goes against the peer_request doc comment, which says debug-port requests are served as before.
✅ All other checks passed
🔒 Security: ✅ LGTM
Peers are still authenticated by the cluster key before the new 503 refusal. The peer port now serves only clustered-actor lookups. The Abort reason is built from an exception this process creates, so it carries no data from the peer.
📖 AGENTS.md: ✅ LGTM
No issues.
⚡ Performance: ✅ LGTM
The new drain logic runs only during stop and is bounded by the close timeout and the connections deadline. Counting each request costs an Rc increment and decrement.
📝 Documentation: ✅ LGTM
The updated ClusterConfig comment in workerd.capnp matches the new drain order.
Reviewed commit: 7dda9f2c
7dda9f2 to
80d71be
Compare
There was a problem hiding this comment.
Superseded by the review of
441b6969.
This review, of 80d71be0
✅ Approved — The one earlier INFO (debug-port requests answered moved from the handoff branch) is fixed, and no new problems were found in the changed code.
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 cluster key still authenticates peers before the new 503 refusal. The peer port now serves only clustered-actor lookups.
📖 AGENTS.md: ✅ LGTM
No issues.
📋 Code Quality: ✅ LGTM
The handoff branch at src/workerd/server/server/actor.rs:973-977 now checks peer instead of remote. Debug-port requests (session == None) are therefore still forwarded after the in_flight deadline, which matches the peer_request doc comment. A moved error from peer_request is returned before any work starts, so the peer can resend the call safely. A session's closing future resolves only after refusing_peers is set and that session's count reaches zero.
⚡ Performance: ✅ LGTM
relays_handed_off is a cheap Instant comparison that runs once per handed-off request. The drain logic runs only during stop, and the close timeout bounds it.
📝 Documentation: ✅ LGTM
The ClusterConfig comment in workerd.capnp describes the drain order the code now follows.
Reviewed commit: 80d71be0
58fc388 to
5e431f3
Compare
80d71be to
441b696
Compare
There was a problem hiding this comment.
Superseded by the review of
44f9ccfd.
This review, of 441b6969
✅ Approved — No findings in this pull request, and the earlier notes are fixed at this commit.
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 exploitable issues found.
- Cluster key:
PeerConnection::connectstill checks the cluster key before the new 503 refusal. - Peer port:
ClusterPeerPortImplimplements onlygetActor, andresolve_peer_actorrejects namespaces that are not clustered. A peer can no longer reach the full debug-port interface. - Peer close: the read-until-close after the Abort is limited to 1 s.
📖 AGENTS.md: ✅ LGTM
No issues.
📋 Code Quality: ✅ LGTM
No problems found in the changed code.
- Refusing calls during stop: once
refusing_peersis set,ClusterNode::peer_requestreturnsmovedbefore any work starts. Debug-port requests (session == None) are still counted and served. - Closing a session: a session's
closingfuture resolves only afterrefusing_peersis set and that session's count reaches zero. - Handoff relay: the
peer = remote && self.session.is_some()check keeps debug-port requests forwarded after thein_flightdeadline. - C++ session close:
ClosingStreamsetsreasonbefore it throws, so the explicit Abort is written before the write side shuts down. The read-until-close is limited byPEER_CLOSE_TIMEOUT.
⚡ Performance: ✅ LGTM
No performance regressions found.
- Per-session counting: the cost per request is negligible.
- Handoff deadline checks: these are cheap
Instantcomparisons, made only for requests that waited on a handoff. - Drain logic: it runs only during stop. The
connectionsdeadline andtimeout_atinrun.rslimit how long it takes.
📝 Documentation: ✅ LGTM
The ClusterConfig comment in workerd.capnp matches the drain order the code follows.
Reviewed commit: 441b6969
441b696 to
44f9ccf
Compare
There was a problem hiding this comment.
Superseded by the review of
aebd6deb.
This review, of 44f9ccfd
✅ Approved — No findings at this commit, and the changes since the previous review (actor.rs, cluster-test.mjs) are 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
- Cluster key:
PeerConnection::connectstill rejects a bad cluster key with 403 before the new 503 refusal. - Peer port:
ClusterPeerPortImplimplements onlygetActor, andresolve_peer_actorrejects namespaces that are not clustered. - Peer close: the read-until-close after the Abort is limited to 1 s, and the connections deadline also bounds it.
📖 AGENTS.md: ✅ LGTM
No issues.
📋 Code Quality: ✅ LGTM
- Refusing calls during stop:
peer_requestreturnsmovedbefore it creates aCounted. Debug-port requests (session == None) are still counted and served. - Counters:
Count::noneandSignal::waitcreate thenotified()future before they check the state, so a wakeup that arrives in between is not lost. - Handoff relay: with
peer = remote && self.session.is_some(), only peer sessions getmovedanswers. The relay deadline is checked after the handoff wait, while resolving the owner, and again as the relay starts. - C++ session close:
ClosingStreamsetsreasonbefore it throws, so the explicit Abort is written before the write side shuts down. The read-until-close is limited byPEER_CLOSE_TIMEOUT. - Tests: the rolling-restart test asserts consecutive counts and a final state of
all + 1. That checks that each call ran exactly once.
⚡ Performance: ✅ LGTM
Per-session counting costs very little per request. The deadline checks are cheap Instant comparisons, and they run only for requests that waited on a handoff.
📝 Documentation: ✅ LGTM
The workerd.capnp comments match the drain order the code follows.
Reviewed commit: 44f9ccfd
A stopping node exited as soon as no peer request it had started was in flight. A peer's call that it had not started yet, still unread on the session or not yet routed to an object, was cut off with the process. The forwarder saw "Peer disconnected", which it treats as a request that may have run, so the client got a 500 instead of a retry. The node now ends its peers' sessions itself, once its own listeners have finished: * It starts no peer request from then on: one that reaches RoutedChannel is answered `moved`, before anything runs, and a new CONNECT is refused with 503, which the dialing peer reports as not delivered. The debug port's requests are still served, and counted until they end. * Each session counts the requests started on it, and closes once that count is zero, so a long-lived request holds only its own session open, up to the connections deadline. * A session closes with `moved` as the reason. A ClosingStream fails its reads, so capnp disconnects with that exception and sends it as an Abort, unless the connection is idle on its side. The node then sends an Abort itself, since an idle side may still have the peer's unread calls, which the peer would otherwise fail as plain disconnects. The peer fails every call it still has out on the session as `moved`, sends each again where the object runs now, and shuts its side. * The node reads on until the peer's EOF, so no unread data turns the close into a reset that could drop the Abort, but for about a second at most, so a peer that never closes holds up the stop little. The KJ timer that bounds it advances only when the loop runs out of work; the connections deadline bounds the stop in any case. Every call the Abort fails was not started: the node starts nothing after the cutoff, and closes a session only once everything started on it has finished. A peer's request that waited here for its object's handoff is sent on to where the object went only until the in-flight deadline, the time work in flight on the object had. Released later, as when a handoff runs late, it is answered `moved` before anything runs, and the peer sends it there itself. Relayed, it could still be running when the connections deadline cuts it off, as a call that may have run, which the peer cannot send again. The deadline is checked again as the relay starts, and bounds resolving where the object went, which can wait on the bucket or a peer's dial: resolving runs nothing on the object, so `moved` is still safe then. Each such answer is logged, as is the start of the handoffs. The debug port's requests are still sent on, since its clients do not retry on `moved`. The cluster port no longer serves the whole debug port. It exposed getEntrypoint (any service, stateless entrypoints included) and getActor for ephemeral and non-clustered namespaces, none of which was counted, so a peer could have started side-effecting work that a stop would then Abort as `moved` after it ran. Peers only ever call getActor on clustered namespaces, so a peer session now gets ClusterPeerPortImpl: getActor for clustered namespaces only, with every request counted on the session, and getEntrypoint unimplemented. The debug port listener is unchanged. The new cluster test runs two lanes of increments, one at a time each, on stubs bound to the stopping node, across its exit, so that a call lands while the node stops. It checks that each call applied exactly once: every call succeeds, the counts have no repeats or gaps, and the final count matches. It also holds an idle session to the stopping node, and checks that the node's own Abort carries the reason. Without this change, with that idle check left out, it fails in 20 of 20 runs: 18 on "Network connection lost", 2 on a node that never found a gap to exit in. With it, it passes 20 of 20. Another test holds a request on a DO past the in-flight deadline, so that its handoff ends late. Once the DO reports the request running on the stopping node, and that node logs that its handoffs started, the test sends a peer's request that waits for the handoff. It checks from the stopping node's log that the peer's request was answered `moved`, and that it ran where the DO went. Relayed instead, it fails in 10 of 10 runs on "Network connection lost"; with the `moved` answer it passes 40 of 40.
5e431f3 to
a0d00cc
Compare
44f9ccf to
aebd6de
Compare
There was a problem hiding this comment.
✅ Approved — No findings at this commit, and the changes since the previous review (actor.rs, cluster-test.mjs) are 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
- Cluster key:
PeerConnection::connectstill rejects a bad cluster key with 403 before the new 503 refusal. - Peer port:
ClusterPeerPortImplimplements onlygetActor, andresolve_peer_actorrejects namespaces that are not clustered. - Peer close: the read-until-close after the Abort is limited to 1 s, and the connections deadline also bounds it.
📖 AGENTS.md: ✅ LGTM
No issues.
📋 Code Quality: ✅ LGTM
- Refusing calls during stop:
peer_requestreturnsmovedbefore it creates aCounted. Debug-port requests (session == None) are still counted and served. - Counters:
Count::noneandSignal::waitcreate thenotified()future before they check the state, so a wakeup that arrives in between is not lost. - Handoff relay: with
peer = remote && self.session.is_some(), only peer sessions getmovedanswers. The relay deadline is checked after the handoff wait, while resolving the owner, and again as the relay starts. - C++ session close:
ClosingStreamsetsreasonbefore it throws, so the explicit Abort is written before the write side shuts down. The read-until-close is limited byPEER_CLOSE_TIMEOUT. - Tests: the rolling-restart test asserts consecutive counts and a final state of
all + 1. That checks that each call ran exactly once.
⚡ Performance: ✅ LGTM
Per-session counting costs very little per request. The deadline checks are cheap Instant comparisons, and they run only for requests that waited on a handoff.
📝 Documentation: ✅ LGTM
The workerd.capnp comments match the drain order the code follows.
Reviewed commit: aebd6deb — same changes as 44f9ccfd, so this is that commit's review (no new review was run; line numbers are as of 44f9ccfd)
ry
left a comment
There was a problem hiding this comment.
[AI] LGTM. I traced the exactly-once argument and it holds:
- Calls are counted at
start_request. - Once the node is refusing, a call is answered
movedbefore anything runs. - Calls that were never read get the Abort.
- A session's count drops only when the peer releases the call's capability (
kjToCapnp(startRequest(...))), so zero means the peer is done with every call that started.
Non-blocking:
- The hand-written
rpc::MessageAbort (needed because capnp sends none on an idle connection) ties this code toTwoPartyVatNetwork's framing. An upstream capnp option to always send the Abort would be cleaner; maybe worth asking Kenton. - At the connections deadline, a session held open by a WebSocket or a returned RpcTarget is dropped without an Abort. A call on it that was never read then fails as a plain disconnect and isn't resent. The window is small; I'm mentioning it so it's a known limit.
PEER_CLOSE_TIMEOUTruns on the KJ timer, so it starves the same way the lease does (the comment says so).resolve_peer_actorrepeatsresolve_debug_actor's namespace lookup; one helper would do.
This makes rolling restarts preserve exactly-once execution for peer
calls. Previously, a call sent but not started could die with the
stopping node, returning 500
Network connection lostwithout retry.This caused cluster-test CI flakes on TSan, ASan and coverage.
A stopping node refuses new peer calls as
movedand newCONNECTswith 503. It drains started calls per session, sends an Abort carrying
moved, then reads for peer EOF for at most about one second. Theconnection deadline still bounds shutdown.
Counts are per session, so a long-lived request such as a WebSocket
holds only its own session open, up to the connections deadline.
Requests waiting for an object's handoff are relayed only within the
in-flight deadline, including owner resolution and relay startup.
Expired requests receive
movedbefore running, so peers resend them.Debug-port requests are always relayed. During shutdown they remain
served and counted, while the cluster port serves only clustered-actor
lookups.
Handoff starts and deadline-triggered
movedresponses are logged.The exactly-once test fails 20 of 20 runs without the fix and passes 20
of 20 with it. The handoff-relay test fails 10 of 10 without its fix and
passes 40 of 40 with it. All cluster targets pass 40 runs each on
bazelflare.
Under heavy CPU and disk stress, the rolling-restart test lost requests
in about 12 of 200 runs on main. The remaining losses on the earlier
branch version were the handoff-relay case fixed here.
It applies to main on its own and does not depend on #8564.