GRI-1912: close the WebSocket gracefully on approve/pend/reject #125

Merged
vivy merged 1 commit from fix/GRI-1912-ws-graceful-close into dev 2026-11-11 02:32:57 +00:00
Member

Summary

Closes grimoire-of-digital-signage#123. approve(), pend() and
reject() in crates/server/src/ws.rs wrote their final frame and then
returned, letting the WebSocket (and its TCP stream) drop with no
closing handshake. Linux answers close() with unread bytes still in the
receive buffer with a TCP RST instead of a FIN, and a reset can discard
data the peer already received but had not yet read off its socket
buffer — including the just-written Approved frame and its one-shot
token. The device is then locked out: the token row has already been
consumed, so every later reconnect gets Rejected: already enrolled on another connection.

  • Added close_gracefully(): sends an explicit WebSocket Close frame,
    then drains incoming frames until EOF or a 5s timeout, so the socket
    empties any unread input before it drops and the OS does a normal FIN
    close instead of an RST.
  • Called it at the end of approve(), pend() (after registration is
    read) and reject().

Test plan

  • cargo build -p server --tests clean
  • Added an_approved_device_s_reconnect_ends_with_a_close_frame_not_a_reset
    in crates/server/tests/ws_integration.rs, asserting the server sends
    an explicit Close frame on this path (the RST race itself is
    timing/OS-dependent and not reliably reproducible in an integration
    test; this pins the observable half of the fix)
  • ./scripts/local-gates.sh --rust-only: every gate passed
## Summary Closes grimoire-of-digital-signage#123. `approve()`, `pend()` and `reject()` in `crates/server/src/ws.rs` wrote their final frame and then returned, letting the `WebSocket` (and its TCP stream) drop with no closing handshake. Linux answers `close()` with unread bytes still in the receive buffer with a TCP RST instead of a FIN, and a reset can discard data the peer already received but had not yet read off its socket buffer — including the just-written `Approved` frame and its one-shot token. The device is then locked out: the token row has already been consumed, so every later reconnect gets `Rejected: already enrolled on another connection`. - Added `close_gracefully()`: sends an explicit WebSocket Close frame, then drains incoming frames until EOF or a 5s timeout, so the socket empties any unread input before it drops and the OS does a normal FIN close instead of an RST. - Called it at the end of `approve()`, `pend()` (after registration is read) and `reject()`. ## Test plan - [x] `cargo build -p server --tests` clean - [x] Added `an_approved_device_s_reconnect_ends_with_a_close_frame_not_a_reset` in `crates/server/tests/ws_integration.rs`, asserting the server sends an explicit Close frame on this path (the RST race itself is timing/OS-dependent and not reliably reproducible in an integration test; this pins the observable half of the fix) - [x] `./scripts/local-gates.sh --rust-only`: every gate passed
GRI-1912: close the WebSocket gracefully on approve/pend/reject
Some checks failed
CI / the pre-push hook (pull_request) Successful in 7s
CI / cargo fmt (pull_request) Successful in 6s
CI / cargo deny (pull_request) Successful in 34s
CI / cargo deny advisories (pull_request) Failing after 33s
CI / repository boundary (pull_request) Successful in 7s
CI / cargo clippy (pull_request) Successful in 9m59s
CI / cargo test (pull_request) Successful in 11m23s
CI / frontend-web (pull_request) Successful in 10m5s
CI / Notify gate failed (pull_request) Has been skipped
CI / CI (pull_request) Successful in 4s
e7a052614c
approve(), pend() and reject() wrote their final frame and then let
the socket drop with no WebSocket close handshake. Linux answers
close() with unread bytes still in the receive buffer with a TCP RST
instead of a FIN, and a reset can discard data the peer already
received but had not yet read off its socket buffer -- including the
just-sent Approved frame and one-shot token.

Add close_gracefully(), which sends an explicit Close frame and drains
incoming frames until EOF or a 5s timeout, and call it at the end of
each of the three replies.
vivy merged commit b16f02bf5f into dev 2026-11-11 02:32:57 +00:00
vivy deleted branch fix/GRI-1912-ws-graceful-close 2026-11-11 02:32:58 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Grimoirix/grimoire-of-digital-signage!125
No description provided.