Skip to content

fix(subscriptions): bound the WebSocket close frame and name a dropped client's reason - #3417

Open
angela-helios wants to merge 5 commits into
mainfrom
fix/2723-ws-close-deadlines
Open

angela-helios wants to merge 5 commits into
mainfrom
fix/2723-ws-close-deadlines

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #2723

This branch is stacked on PR #3347 (#3069) and PR #3322 (#2816), and its diff includes both until they merge. Those two conflict with each other in crates/rest/src/handlers/ws.rs; this branch carries the resolution, as its own merge commit.

Behaviour change to review

  • A bound client that does not accept a frame in time, or whose queue overflows, now receives close code 1008 with the reason client-too-slow. Before, the connection dropped without a close frame (1006). The server tries for 5 seconds; a client that still does not read is dropped without one, as before.
  • A bound client is removed from the manager, and its queued notifications are freed, before its close frame is sent. A stalled client's place under the 16-client cap is free up to 5 seconds earlier than its socket is.
  • Three new variables, with the current values as defaults: HFS_SUBSCRIPTION_WS_BIND_TIMEOUT_MS (10000), HFS_SUBSCRIPTION_WS_SEND_TIMEOUT_MS (30000), HFS_SUBSCRIPTION_WS_CLIENT_QUEUE_CAPACITY (256). Zero is replaced by the default with a warning.

Summary

All five items of the issue:

  • Close frame deadline. Every close frame goes through one function bounded by CLOSE_TIMEOUT (5 s), including single-bind-only and the refusals before the bind.
  • Close reason for a dropped client. 1008 client-too-slow. The manager marks a client it drops on a full queue, so a client whose subscription was deleted is not called too slow; that case still ends without a close frame. A client the manager released because its credential expired gets credential-expired even when it sees the release before its own timer.
  • README replay wording. The README now says $events is id-only, limited to HFS_SUBSCRIPTION_EVENT_HISTORY_SIZE (100 by default, below the 256-notification queue) and in memory.
  • Configurable limits. The three variables above, in both README tables, the work-with-subscriptions skill (both mirrors) and the list of known variables. The send deadline still applies per frame whatever its size; an operator can raise it.
  • Handler-level tests. crates/rest/tests/ws_client_deadlines.rs, next to the other tests of this handler rather than in websocket_integration, which does not reach the handler.

1008 rather than 1013, to match the other reasons of this route.

Tests

Results:

  • cargo test -p helios-subscriptions --lib: 258 passed. --test websocket_integration: 9 passed.
  • cargo test -p helios-rest --lib with the subscriptions feature: 1149 passed. --test ws_client_deadlines: 5 passed. --test ws_bind_credential_expiry, --test ws_unbound_connection_cap and --test ws_bind_message_size: 3 passed each.
  • cargo test -p helios-hfs --bin hfs env_names: 7 passed.
  • I did not run clippy locally; CI runs it.

These ran on current main with the two branches this one is stacked on and one other pending fix (#3111).

…ires

The bind handler authorized a client once at the upgrade and then
streamed to it for as long as the connection lived. It now captures the
expiry of the request's bearer token and closes the stream with
1008 credential-expired at that time. The websocket manager keeps the
deadline with each client and drops a client past it on the next
broadcast or bind.

Without authentication there is no token and no deadline.

Closes #2816
/ws/subscriptions/bind upgraded every request, so a peer could hold any
number of connections that never send a bind message, and the memory
they used stayed in the allocator arenas after they closed.

The engine now holds one slot per connection waiting to bind
(HFS_SUBSCRIPTION_WS_MAX_UNBOUND_CONNECTIONS, default 256). An upgrade
past the limit is refused with 503 and Retry-After. The slot is given
back when the bind message arrives, the connection closes, or the bind
deadline passes. Every 64 closed connections the allocator is asked to
return free memory to the OS.

Closes #3069
…expiry' into fix/2723-wip

# Conflicts:
#	crates/rest/src/handlers/ws.rs
…d client's reason

Every close frame is sent within 5 seconds or given up on. A bound client that
misses the send deadline or overflows its queue is closed with
1008 client-too-slow instead of a dropped connection. The bind deadline, send
deadline and client queue size are configurable through HFS_SUBSCRIPTION_WS_*,
and the README says what $events can and cannot recover.

Closes #2723
@claude

claude Bot commented Dec 2, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebSocket subscription hardening: deadline on the close frame, close code when a client is dropped, replay wording, configurable limits

1 participant