Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion server/src/ws/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ async fn handle_socket(socket: WebSocket, st: AppState, user_id: String) {
}
}

// Only THIS connection: a newer one for the account must stay live. Removed
// BEFORE the writer stops, so no push is accepted here and then dropped
// unwritten - it goes to the account's next open connection instead.
st.online.remove(&user_id, &tx);
writer.abort();
st.online.remove(&user_id);
}

fn base64_content(bytes: &[u8]) -> String {
Expand Down
109 changes: 96 additions & 13 deletions server/src/ws/state.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,119 @@
// In-memory online map: user_id -> outbound channel. NEVER persisted. Cleared
// on disconnect. Used to push messages to currently-connected recipients.
// In-memory online map: user_id -> the account's open connections, newest last.
// NEVER persisted or logged; a connection is listed only while its socket is open.
//
// ponytail: one connection per user (a reconnect replaces the previous sender).
// Multi-device fan-out lands when device linking does.
// Live pushes go to the NEWEST connection only - the single-mailbox model
// (KNOWN_LIMITATIONS): an incoming message lands on whichever device acks it
// first, so fanning it out to every connection would let one that can't decrypt
// it ack (delete) it before the one that can. When a connection closes it removes
// only ITSELF, and the newest one still open takes over live delivery - an older
// socket closing must never cut off a newer one, and the newest closing must hand
// delivery back to one that's still open.

use dashmap::mapref::entry::Entry;
use dashmap::DashMap;
use tokio::sync::mpsc::error::SendError;
use tokio::sync::mpsc::UnboundedSender;

#[derive(Default)]
pub struct Online {
map: DashMap<String, UnboundedSender<String>>,
map: DashMap<String, Vec<UnboundedSender<String>>>,
}

impl Online {
pub fn new() -> Self {
Self::default()
}

/// A connection opened: it becomes the account's live-delivery target.
pub fn insert(&self, user_id: &str, tx: UnboundedSender<String>) {
self.map.insert(user_id.to_string(), tx);
self.map.entry(user_id.to_string()).or_default().push(tx);
}

pub fn remove(&self, user_id: &str) {
self.map.remove(user_id);
/// A connection closed: drop just that one (the entry goes once none are left).
pub fn remove(&self, user_id: &str, tx: &UnboundedSender<String>) {
if let Entry::Occupied(mut e) = self.map.entry(user_id.to_string()) {
e.get_mut().retain(|c| !c.same_channel(tx));
if e.get().is_empty() {
e.remove();
}
}
}

/// Best-effort push to a connected user. Returns false if offline or the
/// channel is closed.
/// Best-effort push to the account's newest open connection (falling back to
/// an older one if the newest is already closing). Returns false if none took
/// it - the message is persisted first, so it still arrives on the next connect.
pub fn send(&self, user_id: &str, msg: String) -> bool {
match self.map.get(user_id) {
Some(tx) => tx.send(msg).is_ok(),
None => false,
let Some(conns) = self.map.get(user_id) else {
return false;
};
let mut msg = msg;
for tx in conns.iter().rev() {
match tx.send(msg) {
Ok(()) => return true,
Err(SendError(back)) => msg = back,
}
}
false
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

#[cfg(test)]
mod tests {
use super::Online;
use tokio::sync::mpsc::unbounded_channel;

const U: &str = "px_00000000000000000000000000000001";

#[test]
fn pushes_go_to_the_newest_connection() {
let online = Online::new();
let (a, mut ra) = unbounded_channel();
let (b, mut rb) = unbounded_channel();
online.insert(U, a);
online.insert(U, b);
assert!(online.send(U, "m".into()));
assert_eq!(rb.try_recv().unwrap(), "m");
assert!(ra.try_recv().is_err(), "only the newest is live");
}

#[test]
fn an_older_connection_closing_keeps_the_newer_live() {
let online = Online::new();
let (a, _ra) = unbounded_channel();
let (b, mut rb) = unbounded_channel();
online.insert(U, a.clone());
online.insert(U, b);
online.remove(U, &a);
assert!(online.send(U, "m".into()));
assert_eq!(rb.try_recv().unwrap(), "m");
}

#[test]
fn the_newest_closing_hands_delivery_back() {
let online = Online::new();
let (a, mut ra) = unbounded_channel();
let (b, _rb) = unbounded_channel();
online.insert(U, a);
online.insert(U, b.clone());
online.remove(U, &b);
assert!(online.send(U, "m".into()));
assert_eq!(ra.try_recv().unwrap(), "m");
}

#[test]
fn a_dead_newest_falls_through_and_the_last_close_clears_the_entry() {
let online = Online::new();
let (a, mut ra) = unbounded_channel();
let (b, rb) = unbounded_channel();
online.insert(U, a.clone());
online.insert(U, b.clone());
drop(rb); // newest socket already gone, not yet removed
assert!(online.send(U, "m".into()));
assert_eq!(ra.try_recv().unwrap(), "m");

online.remove(U, &b);
online.remove(U, &a);
assert!(!online.send(U, "m".into()), "offline once every connection closed");
assert!(online.map.is_empty(), "no entry left behind");
}
}
60 changes: 60 additions & 0 deletions server/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,66 @@ async fn server_end_to_end() {
"should receive a heartbeat ping"
);

// Several connections for one account (tabs / devices): live pushes go to the
// newest; an OLDER one closing must not cut the newer off, and the newest
// closing hands live delivery back to one still open (ws/state.rs).
{
let push = |label: &str| {
let mut c = vec![0u8; 64];
getrandom::getrandom(&mut c).unwrap();
let body = b64.encode(&c);
let (http, base, token, to) = (http.clone(), base.clone(), token.clone(), bob.user_id.clone());
let label = label.to_string();
async move {
let r = http
.post(format!("{base}/messages/send"))
.header("X-Privex-Auth", &token)
.json(&serde_json::json!({ "recipient_id": to, "content": body }))
.send()
.await
.unwrap();
assert!(r.status().is_success(), "send {label}");
body
}
};
let settle = || tokio::time::sleep(std::time::Duration::from_millis(300));
// Bounded: read_until_message keeps answering pings, so a push that never
// comes would otherwise wait forever instead of failing.
let within = std::time::Duration::from_secs(10);
let t_older = ws_ticket(&http, &base, &bob_token).await;
let mut older = ws_connect(addr, &t_older).await.expect("older connects");
let t_newer = ws_ticket(&http, &base, &bob_token).await;
let mut newer = ws_connect(addr, &t_newer).await.expect("newer connects");
settle().await;

older.close(None).await.unwrap();
drop(older);
settle().await;
let sent = push("after the older closed").await;
let got = tokio::time::timeout(within, read_until_message(&mut newer))
.await
.expect("newer got nothing live after the older closed");
assert_eq!(got["content"].as_str().unwrap(), sent, "newer still live after the older closed");
let ack = |m: &serde_json::Value| {
WsMessage::Text(serde_json::json!({ "type": "ack", "message_ids": [m["message_id"]] }).to_string())
};
newer.send(ack(&got)).await.unwrap();

let t_newest = ws_ticket(&http, &base, &bob_token).await;
let mut newest = ws_connect(addr, &t_newest).await.expect("newest connects");
settle().await;
newest.close(None).await.unwrap();
drop(newest);
settle().await;
let sent = push("after the newest closed").await;
let got = tokio::time::timeout(within, read_until_message(&mut newer))
.await
.expect("nothing handed back live after the newest closed");
assert_eq!(got["content"].as_str().unwrap(), sent, "delivery handed back after the newest closed");
newer.send(ack(&got)).await.unwrap();
settle().await;
}

// ===== Session 11: KT log + key management =====
let kt_pub = SigningKey::from_bytes(&[9u8; 32]).verifying_key();
let count_opks = |uid: String| {
Expand Down
Loading