Repository navigation
WebSocket Server Deduplication - Single Node - #183
Conversation
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.
|
Caution Review failedFailed 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 ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
💤 Files with no reviewable changes (1)
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)
📝 WalkthroughWalkthroughThe 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. ChangesWebSocket and IPC runtime behavior
Estimated code review effort: 4 (Complex) | ~60 minutes Merge Risk: 🟠 High · up to 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
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
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-clientacknowledges 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_seenmaps onto a RedisSET NX EXdirectly, 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
Checklist
cargo check/cargo testpass locally (for Rust 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_helpers304 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
acts_once_on_a_message_the_client_sent_twice_but_answers_bothdrives 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
Bug Fixes
Tests