Skip to content

fix(rest): send large WebSocket notifications as bounded frames - #3422

Open
angela-helios wants to merge 7 commits into
mainfrom
fix/3078-ws-fragment-large-notifications
Open

angela-helios wants to merge 7 commits into
mainfrom
fix/3078-ws-fragment-large-notifications

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3078

This branch is stacked on PR #3421 (#3080), and its diff includes the commits of that branch, and of the branches it is stacked on, until they merge.

Behaviour change to review

  • A notification over 64 KiB is sent as one text message split into frames of at most 64 KiB (RFC 6455 fragmentation). WebSocket libraries reassemble it; a client that parses frames itself must handle continuation frames. Smaller notifications are sent as one frame, as before.
  • A client closed in the middle of a large notification receives the close frame after the frame being sent, not after the whole notification.
  • helios-rest gets direct dependencies on tokio-tungstenite, hyper and hyper-util, at the versions axum already brings in. No package is added to Cargo.lock.

Summary

tungstenite copies each outgoing frame into a write buffer that it never shrinks, so every bound connection kept a buffer the size of its largest notification.

  • Large notifications are sent as 64 KiB fragments, each flushed before the next is buffered, so the buffer stays near the fragment size. The fragments share the bytes of the one serialized payload.
  • axum's WebSocket sends whole messages only, so the bind route now runs tokio_tungstenite on the upgraded connection. The request is still validated by axum's WebSocketUpgrade, and the limits on incoming messages are the same.
  • The send deadline still covers the whole notification.
  • The crate README and the work-with-subscriptions skill (both mirrors) describe the fragmentation.

A size limit on full-resource notifications, also suggested in the issue, is not part of this change.

Tests

  • crates/rest/tests/ws_large_notifications.rs: reads the frames off the wire and checks that none is over 64 KiB, that they make up the notification and that a small one is a single frame; a client receives large notifications, with frames ending inside a character, as one message.
  • handlers/ws.rs: how a message is split, and that the frames do not copy it.

Results:

  • cargo test -p helios-rest --lib with the subscriptions feature: 1151 passed. --test ws_large_notifications: 2 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.
  • The memory figures of the issue were not measured again.
  • I did not run clippy locally; CI runs it.

These ran on current main with the branches this one is stacked on.

…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
tungstenite keeps a write buffer the size of the largest frame it has sent, so
every bound connection held a copy of its largest notification. A notification
over 64 KiB is now one message in frames of at most 64 KiB, each flushed before
the next. axum's WebSocket cannot send frames, so the bind route runs
tokio-tungstenite on the upgraded connection.

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

Each bound WebSocket connection keeps a write buffer the size of its largest notification, so a large notification stays resident once per client

1 participant