heartbeat Ping can follow closing #34

Open
opened 2026-11-15 13:18:30 +01:00 by SirStone · 1 comment
Owner

Summary

closeRegistered removes the connection from the registry and then sends closing + the close frame, so that nothing can be enqueued behind the reason. The heartbeat thread, however, takes a registry snapshot and pings every connection in it with no closed-state check at send time. A snapshot taken just before the removal lets a Ping be enqueued after closing, which the protocol forbids: the last thing a client sees before the close must be the reason.

Where

  • server/main.nim:117-123 — closeRegistered: registry removal, then closing, then ws.close().
  • server/main.nim:2056-2062 — heartbeat: for conn in app().conns.snapshot(): conn.ws.send(payload, Ping) with no closed/registry re-check.

Failure scenario

The GM revokes a player key while a heartbeat tick is in flight. The heartbeat snapshots the registry, the revocation's closeRegistered runs to completion, and the heartbeat then sends a Ping on the now-closing socket. The client sees closing, then a Ping, which either arrives after WebSocket close (dropped) or is delivered first, so the reason is no longer last. The invariant "closing is the terminal frame" holds only by timing, not by construction — an otherwise harmless race that a stricter client will flag.

Confidence

High on the race; low impact.

Fix direction

Make the closing invariant structural: every outbound enqueue consults a per-connection closed flag (set before closing is sent, under the connection's own lock), so a heartbeat that lost the race drops its ping instead of enqueuing it. Alternatively, send all frames through one guarded send path that refuses anything after closing.

Acceptance

  • No frame of any kind can be enqueued after closing on a connection, regardless of sender.
  • A stress test that closes connections while the heartbeat runs never observes a frame after the reason.
  • Ordinary closure paths still deliver closing then the close frame, in that order, exactly once.

Related

  • #23, #25 (slice 8, slice 10).
  • #2 (M3 planning map).
## Summary `closeRegistered` removes the connection from the registry and then sends `closing` + the close frame, so that nothing can be enqueued behind the reason. The heartbeat thread, however, takes a registry snapshot and pings every connection in it with no closed-state check at send time. A snapshot taken just before the removal lets a `Ping` be enqueued after `closing`, which the protocol forbids: the last thing a client sees before the close must be the reason. ## Where - `server/main.nim:117-123` — `closeRegistered`: registry removal, then `closing`, then `ws.close()`. - `server/main.nim:2056-2062` — `heartbeat`: `for conn in app().conns.snapshot(): conn.ws.send(payload, Ping)` with no `closed`/registry re-check. ## Failure scenario The GM revokes a player key while a heartbeat tick is in flight. The heartbeat snapshots the registry, the revocation's `closeRegistered` runs to completion, and the heartbeat then sends a `Ping` on the now-closing socket. The client sees `closing`, then a `Ping`, which either arrives after WebSocket close (dropped) or is delivered first, so the reason is no longer last. The invariant "closing is the terminal frame" holds only by timing, not by construction — an otherwise harmless race that a stricter client will flag. ## Confidence High on the race; low impact. ## Fix direction Make the closing invariant structural: every outbound enqueue consults a per-connection closed flag (set before `closing` is sent, under the connection's own lock), so a heartbeat that lost the race drops its ping instead of enqueuing it. Alternatively, send all frames through one guarded send path that refuses anything after `closing`. ## Acceptance - No frame of any kind can be enqueued after `closing` on a connection, regardless of sender. - A stress test that closes connections while the heartbeat runs never observes a frame after the reason. - Ordinary closure paths still deliver `closing` then the close frame, in that order, exactly once. ## Related - #23, #25 (slice 8, slice 10). - #2 (M3 planning map).
SirStone added the needs-triage label 2026-11-15 13:18:30 +01:00
Author
Owner

This was generated by AI during triage.

Confirmed. The race is real: the heartbeat takes a registry snapshot and pings every connection in it without re-checking the closed state at send time, while the closing path removes the connection from the registry and then sends closing and the close frame. A snapshot taken just before the removal lets a Ping be enqueued behind the reason, so "closing is the last thing the client sees" holds only by timing.

Owner decision (triage of this issue): this is fixed as part of the M5 bounded outbox work, not separately. The fix direction the body names — every outbound enqueue consults a per-connection closed flag, i.e. one guarded send path that refuses anything after closing — is exactly the send path M5 introduces (per-connection outbox, coalescing, stop-feeding, forced close). Building a second guard now would be replaced by that work, and would leave two paths to keep consistent in the meantime.

Why not delegable now: the fix has no meaning until the single outbound path exists; it is a property of that path, not of the heartbeat. Doing it first would either duplicate M5's design or be thrown away.

Exposure while it waits (accepted): client-invisible in practice. A WS Ping is a control frame the browser does not expose to application code, so no browser client can observe a Ping arriving after the reason; the only observer is a raw-socket client, and what it sees is a conformance nit on the wire, not leaked state, not a missing reason, and not a wrong close outcome. No data, no ordering guarantee the client relies on, and no protocol version change is affected.

Keep this issue open as the record so the M5 outbox work is not considered done without it: the acceptance stays "no frame of any kind can be enqueued after closing, regardless of sender, demonstrated by a stress test that closes connections while the heartbeat runs, with the ordinary closure path still delivering closing then the close frame exactly once". It should be closed by the change that lands the guarded send path, not before.

No design change is needed: DESIGN already states the invariant (a server-initiated close is closing then the close frame, and closing is the last app frame that connection ever sees); what is missing is that the invariant is enforced by construction rather than by timing, and that enforcement belongs to M5's outbox.

> *This was generated by AI during triage.* Confirmed. The race is real: the heartbeat takes a registry snapshot and pings every connection in it without re-checking the closed state at send time, while the closing path removes the connection from the registry and then sends `closing` and the close frame. A snapshot taken just before the removal lets a `Ping` be enqueued behind the reason, so "`closing` is the last thing the client sees" holds only by timing. **Owner decision (triage of this issue):** this is fixed as part of the M5 bounded outbox work, not separately. The fix direction the body names — every outbound enqueue consults a per-connection closed flag, i.e. one guarded send path that refuses anything after `closing` — is exactly the send path M5 introduces (per-connection outbox, coalescing, stop-feeding, forced close). Building a second guard now would be replaced by that work, and would leave two paths to keep consistent in the meantime. **Why not delegable now:** the fix has no meaning until the single outbound path exists; it is a property of that path, not of the heartbeat. Doing it first would either duplicate M5's design or be thrown away. **Exposure while it waits (accepted):** client-invisible in practice. A WS Ping is a control frame the browser does not expose to application code, so no browser client can observe a Ping arriving after the reason; the only observer is a raw-socket client, and what it sees is a conformance nit on the wire, not leaked state, not a missing reason, and not a wrong close outcome. No data, no ordering guarantee the client relies on, and no protocol version change is affected. **Keep this issue open** as the record so the M5 outbox work is not considered done without it: the acceptance stays "no frame of any kind can be enqueued after `closing`, regardless of sender, demonstrated by a stress test that closes connections while the heartbeat runs, with the ordinary closure path still delivering `closing` then the close frame exactly once". It should be closed by the change that lands the guarded send path, not before. No design change is needed: DESIGN already states the invariant (a server-initiated close is `closing` then the close frame, and `closing` is the last app frame that connection ever sees); what is missing is that the invariant is enforced by construction rather than by timing, and that enforcement belongs to M5's outbox.
SirStone added ready-for-humanbug and removed needs-triage labels 2026-11-15 15:41:27 +01:00
Sign in to join this conversation.