Skip to content

WebSocket Server Deduplication - Single Node - #183

Merged
fr3shw3b merged 2 commits into
mainfrom
feat/ws-server-dedupe
Aug 17, 2026
Merged

fr3shw3b merged 2 commits into
mainfrom
feat/ws-server-dedupe

Conversation

@fr3shw3b

@fr3shw3b fr3shw3b commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

A message a client sent twice was acted on twice.

A client resends when it does not hear an acknowledgement in time, and what went missing may have been the acknowledgement rather than the message. The runtime kept no memory of what it had already handled, so the second copy went to the handler like any other. An application that took a payment or sent a notification did it again.

Message ids a client gave are now remembered for five minutes, and a message arriving under an id already seen is answered but not handed to a handler a second time.

The order of those two is the whole thing

Both halves have to be there, in that order, or one of two failures follows.

Answering without remembering runs the handler twice, which is the bug above.

Remembering without answering never ends. The client resends precisely because it heard nothing, so a duplicate met with silence is one it will send again, and again, until it gives up and reports the message lost. A message that arrived the first time gets reported as lost.

The acknowledgement goes out when the message is taken in, in the read loop, and the check happens when it is handled, in the worker. So a duplicate is answered and then dropped, which is the order that satisfies both. The test asserts both halves, and each was checked by breaking it: removing the check fails it, and moving the check ahead of the acknowledgement fails it by timing out on an answer that never comes, which is that resend loop in miniature.

This is the same rule as on the client side, where celerity-ws-client acknowledges a message before its own deduplication suppresses it, for the same reason in the other direction.

Decisions worth reviewing

Only ids the client gave are remembered. A message that carries none gets one from the runtime, which is unique by construction, so it could never match anything. Remembering those would fill the store with entries nothing can ever look up.

Recording and checking are one call, not a check and then a write, so two copies arriving together cannot both find the id absent and both be handled.

Expiry is decided by the entry, not by the sweep. An id past its time counts as absent whether or not it has been swept, so nothing depends on the sweep for correctness. It only bounds memory.

The store is built in setup but its sweep starts in run. Sweeping spawns, and setup is called from outside a runtime in the FFI call mode. That is the crash fixed in the branch below this one, avoided here by construction rather than rediscovered.

What this does not cover

A cluster. The specification asks for a shared store there, and this is in memory. A client that reconnects may land on a different node and resend, and this node's memory says nothing about what another one has seen, so the duplicate would be handled again.

The store sits behind a trait shaped for that, record_and_check_seen maps onto a Redis SET NX EX directly, but the implementation needs cluster machinery that is dormant: no broadcaster is wired, the inter-node worker never starts, and there is no Redis configuration to point it at. That is its own piece of work and is on the follow up list, blocked on the same wiring the cluster acknowledgement path needs.

So this is single node conformance, and worth reading as that rather than as the deduplication section closed out.

Also in here

The IPC end to end tests are split by what they serve. One file had grown to two thousand lines covering every application type, so the tests for any one of them were read past the rest. They are now four: WebSocket messages, handlers invoked by name, HTTP requests, and the handler stream itself, which is not an application type but the socket the others arrive over.

The harness moves to the module the tests already share. Each of those files compiles as its own crate, so the harness is included by each rather than linked once, which makes anything a given file does not use look unused to the compiler. Unused items are allowed there for that reason, deliberately, and for test support code only.

No test changed. The same tests, the same words, and the same count, which is the thing worth checking in a move like this since a dropped test just quietly stops running.

Type of change

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring (no functional changes)
  • Documentation
  • CI/build configuration
  • Other (describe below)

Checklist

  • Commits follow the commit guidelines (conventional commits, imperative mood, appropriate scope)
  • Changes are scoped to the relevant component(s) and avoid unrelated modifications
  • Tests have been added or updated to cover the changes
  • Documentation has been updated where applicable (the specification already requires this; nothing there needed changing)
  • cargo check / cargo test pass locally (for Rust changes)
  • No secrets, credentials, or sensitive data are included in the changes

Testing

cargo fmt --all -- --check
cargo clippy --workspace --exclude celerity-python-runtime-sdk --all-targets -- -D warnings
cargo test -p celerity_runtime_core -p celerity_ws_registry -p celerity_helpers

304 tests, run three consecutive times to catch ordering and timing flakes. The count is the same before and after the test split, which is what says nothing was lost in the move, and each of the four new files also passes on its own.

Coverage added

  • The store has four tests: new the first time and seen the second, ids told apart from each other, an id forgotten once its time is up, and a sweep that removes what has expired while keeping what has not.
  • acts_once_on_a_message_the_client_sent_twice_but_answers_both drives it end to end over IPC, requiring one dispatch and two acknowledgements. Checked against both failure modes rather than one, as described above.

Summary by CodeRabbit

  • New Features

    • Added WebSocket duplicate-message suppression using client-provided message IDs.
    • Messages without IDs continue to be processed normally.
    • Duplicate detection applies consistently to text and binary messages.
  • Bug Fixes

    • Prevents repeated WebSocket messages from being processed more than once within the configured retention period.
  • Tests

    • Expanded integration coverage for WebSocket deduplication, acknowledgements, authentication, ordering, throughput, HTTP handling, invocation, security, and connection contention.

A client resends when it does not hear an acknowledgement in time, and what
went missing may have been the acknowledgement rather than the message. The
runtime had no memory of what it had already handled, so the second copy was
handled again, and an application that took money or sent a notification did it
twice.

Message ids a client gave are now remembered for five minutes, and a message
arriving under an id already seen is answered but not handed to a handler
again. Ids the runtime made up for messages that carried none are not
remembered, since they are unique by construction and would only fill the store
with entries nothing can look up.

The order of those two matters in both directions. Answering without
remembering runs the handler twice. Remembering without answering leaves the
client resending a message the runtime has decided to say nothing about, which
never ends. The acknowledgement goes out when the message is taken in and the
check happens when it is handled, so a duplicate is answered and dropped.

Recording and checking are one call rather than a check and then a write, so
two copies arriving together cannot both find the id absent and both be
handled.

This covers a single node. A cluster needs a shared store, because a client
that reconnects may land on a different node and resend there, and this node's
memory says nothing about what another one has seen. The store is behind a
trait so that one can take its place, but it cannot be written until there is
cluster machinery to configure it against.
One file had grown to two thousand lines covering every application type the
runtime serves, so finding the tests for one of them meant reading past all the
others, and adding to any of them meant editing the file everything else lives
in.

They are now four, one for each way work reaches a handler. WebSocket messages,
handlers invoked by name, HTTP requests, and the handler stream itself, which is
not an application type at all but the socket everything else arrives over and
so belongs on its own rather than folded into one of them.

The harness moves to the common module the tests already share. Each of these
files compiles as its own crate, so the harness is included by each rather than
linked once, which means anything one file does not use looks unused to the
compiler. Unused items are allowed there for that reason, which is a deliberate
loosening rather than an oversight, and it applies to test support code only.

Nothing else changed. The same tests, in the same words, and the same count of
them.
@coderabbitai

coderabbitai Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Failed to post review comments.

GitHub was unavailable or timed out while CodeRabbit was posting the review. Please request a new review later if the pull request still needs one. This happened while posting 4 inline comments. Use @coderabbitai full review to retry the review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 434a4ad7-9d41-4c00-a1ae-0c21cd1eeb56

📥 Commits

Reviewing files that changed from the base of the PR and between 6f956e9 and 04fccb4.

📒 Files selected for processing (11)
  • libs/runtime/core/src/application.rs
  • libs/runtime/core/src/lib.rs
  • libs/runtime/core/src/websocket.rs
  • libs/runtime/core/src/websocket_dedupe.rs
  • libs/runtime/core/tests/common/ipc.rs
  • libs/runtime/core/tests/common/mod.rs
  • libs/runtime/core/tests/ipc_end_to_end_test.rs
  • libs/runtime/core/tests/ipc_http_test.rs
  • libs/runtime/core/tests/ipc_invoke_test.rs
  • libs/runtime/core/tests/ipc_stream_test.rs
  • libs/runtime/core/tests/ipc_websocket_test.rs
💤 Files with no reviewable changes (1)
  • libs/runtime/core/tests/ipc_end_to_end_test.rs

Included review availability: Your plan includes up to 1 review per rolling hour; 0 remain after this review.

⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: integrate
📝 Walkthrough

Walkthrough

The runtime adds TTL-based WebSocket message-ID deduplication. Application startup owns the store and eviction task. IPC integration tests are reorganized around shared helpers and expanded coverage for HTTP, invocation, socket handling, and WebSocket behavior.

Changes

WebSocket and IPC runtime behavior

Layer / File(s) Summary
Message-ID store contract and implementation
libs/runtime/core/src/lib.rs, libs/runtime/core/src/websocket_dedupe.rs
The public MessageIdStore API and InMemoryMessageIdStore track IDs atomically, expire them by TTL, and evict expired entries.
WebSocket deduplication flow
libs/runtime/core/src/application.rs, libs/runtime/core/src/websocket.rs
Applications create the store when WebSockets are enabled, start eviction, and share the store with WebSocket state. Text and binary messages with repeated IDs are suppressed.
Shared IPC integration harness
libs/runtime/core/tests/common/ipc.rs, libs/runtime/core/tests/common/mod.rs
Shared helpers start isolated runtimes, connect handler stubs, route IPC events, and construct HTTP and WebSocket outcomes.
HTTP, invocation, and socket integration coverage
libs/runtime/core/tests/ipc_http_test.rs, libs/runtime/core/tests/ipc_invoke_test.rs, libs/runtime/core/tests/ipc_stream_test.rs
Tests cover routing, binary payloads, deadlines, invocation modes, cancellation, socket permissions, and socket contention.
WebSocket IPC integration coverage
libs/runtime/core/tests/ipc_websocket_test.rs
Tests cover routing, framing, acknowledgements, deduplication, authentication, heartbeat, ordering, saturation, and disconnect handling.

Estimated code review effort: 4 (Complex) | ~60 minutes

Merge Risk: 🟠 High · up to 04fcc

The change adds WebSocket message deduplication, but the current implementation can suppress legitimate messages when different clients reuse an ID, retain memory after application shutdown, and grow without a capacity limit under unique-ID traffic. These correctness and availability risks could cause lost work or increasing memory use, so the PR is not safe to merge until addressed.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant WebSocket
  participant MessageIdStore
  participant IPCHandler
  Client->>WebSocket: Send text or binary message with ID
  WebSocket->>MessageIdStore: Check and record message ID
  MessageIdStore-->>WebSocket: Return new or duplicate status
  WebSocket->>IPCHandler: Dispatch new message
  WebSocket-->>Client: Suppress duplicate dispatch
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: single-node WebSocket message deduplication.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/ws-server-dedupe

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fr3shw3b
fr3shw3b merged commit a915735 into main Aug 17, 2026
3 checks passed
@fr3shw3b
fr3shw3b deleted the feat/ws-server-dedupe branch August 17, 2026 18:34
@coderabbitai coderabbitai Bot mentioned this pull request Aug 19, 2026
7 of 13 tasks
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.

1 participant