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.
## 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).
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Summary
closeRegisteredremoves the connection from the registry and then sendsclosing+ 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 aPingbe enqueued afterclosing, 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, thenclosing, thenws.close().server/main.nim:2056-2062—heartbeat:for conn in app().conns.snapshot(): conn.ws.send(payload, Ping)with noclosed/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
closeRegisteredruns to completion, and the heartbeat then sends aPingon the now-closing socket. The client seesclosing, then aPing, 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
closingis 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 afterclosing.Acceptance
closingon a connection, regardless of sender.closingthen the close frame, in that order, exactly once.Related
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
closingand the close frame. A snapshot taken just before the removal lets aPingbe enqueued behind the reason, so "closingis 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 deliveringclosingthen 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
closingthen the close frame, andclosingis 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.