server: fix client switching desync on reconnect - #85
Open
justdoGIT wants to merge 1 commit into
Open
Conversation
Using a Slab for client storage causes client indices to become non-contiguous when clients disconnect and reconnect. Because the switching logic assumed contiguous 0..clients.len() indexing, a reconnected client with key > 0 would fail exists(current) and immediately reset back to the local server (index 0). Replace the Slab with a Vec so client indices are always contiguous and modular rotation safely cycles through all connected clients. Signed-off-by: KK <pandeykamal13526@gmail.com>
Open
There was a problem hiding this comment.
🟡 Changes recommended
The new Vec compaction/removal paths don’t consistently adjust both current and previous, which can lead to incorrect targeting and potential out-of-bounds panics.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes client switching becoming unresponsive after disconnect/reconnect by changing client storage from Slab (non-contiguous keys after removals) to Vec (contiguous indices), so the round-robin switch (current + 1) % (clients.len() + 1) remains valid across reconnects.
Changes:
- Replaced client storage from
Slab<(Sender<_>, SocketAddr)>toVec<(Sender<_>, SocketAddr)>. - Simplified the switch-hotkey logic to a single modulo step (no “exists” scan).
- Adjusted client removal handling when a send fails to keep
currentconsistent with shiftedVecindices.
File summaries
| File | Description |
|---|---|
| rkvm-server/src/server.rs | Switch client bookkeeping from Slab to Vec to keep indices contiguous and make switching reliable across disconnects/reconnects. |
Review details
Suppressed comments (1)
rkvm-server/src/server.rs:259
- When a send fails and you
remove(idx - 1), the vector shifts. The code updatescurrentbut notprevious, even thoughpreviouscan later be used as an index (idx = previouswhilechangedis true). This can leavepreviousout of range and cause an out-of-bounds panic onclients[idx - 1], especially if the removed client waspreviousor was before it.
if clients[idx - 1].0.send(Update::Event { id, event }).await.is_err() {
clients.remove(idx - 1);
if current == idx {
current = 0;
} else if current > idx {
current -= 1;
}
break;
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
65
to
69
| // Remove dead clients. | ||
| clients.retain(|_, (client, _)| !client.is_closed()); | ||
| if !clients.contains(current) { | ||
| clients.retain(|(client, _)| !client.is_closed()); | ||
| if current > clients.len() { | ||
| current = 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #65
Using a
Slabfor client storage causes client indices to become non-contiguous when clients disconnect and reconnect. Because the switching logic assumed contiguous0..clients.len()indexing:A reconnected client assigned a slab key > 0 while earlier slots are vacant would fail
exists(current)and immediately cycle back tocurrent = 0(server), producing the exact symptom described in #65:The switch hotkey becomes completely unresponsive to that client until the server process is restarted.
This PR replaces
Slabwith aVecfor client storage so indices remain contiguous, eliminating index drift and ensuring reliable round-robin switching across disconnects and multiple clients.