Skip to content

fix(subscriptions): close a WebSocket client that stays behind without filling its queue - #3421

Open
angela-helios wants to merge 6 commits into
mainfrom
fix/3080-ws-delivery-deadline
Open

angela-helios wants to merge 6 commits into
mainfrom
fix/3080-ws-delivery-deadline

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3080

This branch is stacked on PR #3417 (#2723, which contains #3347 and #3322), and its diff includes those commits until they merge.

Behaviour change to review

  • A bound WebSocket client with a notification still undelivered 2 minutes after it was queued is closed with 1008 client-too-slow, however few notifications it has waiting. Before, only a full queue or one frame past the send deadline closed it.
  • New variable HFS_SUBSCRIPTION_WS_DELIVERY_TIMEOUT_MS (120000). Zero is replaced by the default with a warning. It also bounds the send of one frame, so raising HFS_SUBSCRIPTION_WS_SEND_TIMEOUT_MS above it needs both raised.

Summary

A client that accepted each frame inside the send deadline, and kept its queue under the cap, could hold up to 255 queued bundles for hours.

  • Each queued notification carries the time it was queued. The handler closes the client when it takes one that is already past the delivery deadline, and gives each send no more than what is left of it. Notifications are sent in the order they were queued, so this bounds every notification behind the one being sent.
  • The close uses the path added for WebSocket subscription hardening: deadline on the close frame, close code when a client is dropped, replay wording, configurable limits #2723: the queue is freed and the client removed first, then the close frame is sent within 5 seconds.
  • This is the queue-age option of the issue. A total-lag timer would also close a client on a busy subscription that takes each notification promptly but whose queue is never empty.
  • Both README tables, the work-with-subscriptions skill (both mirrors) and the list of known variables have the new variable.

Tests

  • crates/rest/tests/ws_client_deadlines.rs: a slow reader under the queue capacity is closed with client-too-slow; a client that keeps up outlives the deadline.
  • handlers/ws.rs: the deadline given to one send.
  • helios-subscriptions: a queued notification keeps the time it was queued; zero and configured timeouts.

Results:

  • cargo test -p helios-subscriptions --lib: 254 passed. --test websocket_integration: 9 passed.
  • cargo test -p helios-rest --lib with the subscriptions feature: 1150 passed. --test ws_client_deadlines: 7 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.
  • The default of 2 minutes is a choice made in this change.
  • I did not run clippy locally; CI runs it.

These ran on current main with the branches this one is stacked on. #3420 (#3077) is stacked on the same branch and edits ws_manager.rs too, so the later one to merge needs its branch updated.

…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
…t filling its queue

A bound client that accepted every frame inside the send deadline and kept its
queue under the cap could hold queued bundles for hours. Each notification must
now be delivered within HFS_SUBSCRIPTION_WS_DELIVERY_TIMEOUT_MS (2 minutes by
default) of being queued, or the client is closed with 1008 client-too-slow.

Closes #3080
@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.

A slow WebSocket subscription reader that stays under the 256-notification queue cap is never dropped and holds its queued bundles for hours

2 participants