From c700a0b52326284377efa87f51b10dfaff6340b0 Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Sun, 18 Oct 2026 08:51:20 +0530 Subject: [PATCH 1/2] fix(sdks/rust): invoke on_disconnect when mutation queued behind disconnect (#6320) Signed-off-by: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> --- sdks/rust/src/db_connection.rs | 79 +++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/sdks/rust/src/db_connection.rs b/sdks/rust/src/db_connection.rs index c1810f7e5dd..b090284363e 100644 --- a/sdks/rust/src/db_connection.rs +++ b/sdks/rust/src/db_connection.rs @@ -329,6 +329,34 @@ impl DbContextImpl { // Set `send_chan` to `None`, since `Self::is_active` checks that. *self.send_chan.lock().unwrap() = None; + /// Send a message to the WebSocket loop, returning the terminal error if the + /// connection can no longer send. + /// + /// Every send failure has to reach [`Self::end_connection`]: otherwise the + /// connection ends without `on_disconnect` running, because the run loops + /// treat a normal-disconnect error as a plain end of the loop. + /// + /// Note the deliberate two-step shape: `end_connection` takes the + /// `send_chan` lock, and `StdMutex` is not reentrant, so the lock has to be + /// released before calling it. + fn try_send(&self, msg: ws::v2::ClientMessage) -> crate::Result<()> { + let sent = { + let mut send_chan = self.send_chan.lock().unwrap(); + match send_chan.as_mut() { + Some(send_chan) => send_chan.unbounded_send(msg).is_ok(), + None => false, + } + }; + if sent { + Ok(()) + } else { + // `None`, not `Some(Disconnected)`: this is a requested or observed + // closure, and the WebSocket-close path reports a clean end to + // `on_disconnect` in the same way. + Err(self.end_connection(None)) + } + } + match lifecycle { ConnectionLifecycle::Connecting => { let callback_error = callback_error.unwrap_or_else(|| crate::Error::FailedToConnect { @@ -380,13 +408,7 @@ impl DbContextImpl { // Register the subscription, so we can handle related messages from the server. inner.subscriptions.register_subscription(query_set_id, handle.clone()); if let Some(msg) = handle.start() { - self.send_chan - .lock() - .unwrap() - .as_mut() - .ok_or(crate::Error::Disconnected)? - .unbounded_send(ws::v2::ClientMessage::Subscribe(msg)) - .expect("Unable to send subscribe message: WS sender loop has dropped its recv channel"); + self.try_send(ws::v2::ClientMessage::Subscribe(msg))?; } // else, the handle was already cancelled. } @@ -404,13 +426,7 @@ impl DbContextImpl { callback(&self.make_event_ctx(())); } PendingUnsubscribeResult::SendUnsubscribe(m) => { - self.send_chan - .lock() - .unwrap() - .as_mut() - .ok_or(crate::Error::Disconnected)? - .unbounded_send(ws::v2::ClientMessage::Unsubscribe(m)) - .expect("Unable to send unsubscribe message: WS sender loop has dropped its recv channel"); + self.try_send(ws::v2::ClientMessage::Unsubscribe(m))?; } } } @@ -437,13 +453,7 @@ impl DbContextImpl { request_id, flags, }); - self.send_chan - .lock() - .unwrap() - .as_mut() - .ok_or(crate::Error::Disconnected)? - .unbounded_send(msg) - .expect("Unable to send reducer call message: WS sender loop has dropped its recv channel"); + self.try_send(msg)?; } // Invoke a procedure: stash its callback, then send the `CallProcedure` WS message. @@ -466,13 +476,7 @@ impl DbContextImpl { request_id, flags: ws::v2::CallProcedureFlags::Default, }); - self.send_chan - .lock() - .unwrap() - .as_mut() - .ok_or(crate::Error::Disconnected)? - .unbounded_send(msg) - .expect("Unable to send procedure call message: WS sender loop has dropped its recv channel"); + self.try_send(msg)?; } // Disconnect: close the connection. @@ -667,7 +671,10 @@ impl DbContextImpl { std::thread::spawn(move || loop { match this.advance_one_message_blocking() { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => return, + Err(e) if error_is_normal_disconnect(&e) => { + this.end_connection(None); + return; + } Err(e) => panic!("{e:?}"), } }) @@ -683,7 +690,10 @@ impl DbContextImpl { loop { match this.advance_one_message_async().await { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => return, + Err(e) if error_is_normal_disconnect(&e) => { + this.end_connection(None); + return; + } Err(e) => panic!("{e:?}"), } } @@ -698,7 +708,14 @@ impl DbContextImpl { loop { match this.advance_one_message_async().await { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => return Ok(()), + Err(e) if error_is_normal_disconnect(&e) => { + // A queued mutation can observe the connection closure before the + // run loop sees the closed message channel (for example a reducer + // queued behind `disconnect()`). Route the terminal state through + // `end_connection` so `on_disconnect` runs exactly once either way. + this.end_connection(None); + return Ok(()); + } Err(e) => return Err(e), } } From dea0d53b85057d7fd16bbf6cab47d4597b4877b5 Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:51:36 +0530 Subject: [PATCH 2/2] fix(sdks/rust): invoke on_disconnect on mutation send failure via end_connection Signed-off-by: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> --- sdks/rust/src/db_connection.rs | 88 +++++++++++++++------------------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/sdks/rust/src/db_connection.rs b/sdks/rust/src/db_connection.rs index b090284363e..3ca3045529a 100644 --- a/sdks/rust/src/db_connection.rs +++ b/sdks/rust/src/db_connection.rs @@ -329,34 +329,6 @@ impl DbContextImpl { // Set `send_chan` to `None`, since `Self::is_active` checks that. *self.send_chan.lock().unwrap() = None; - /// Send a message to the WebSocket loop, returning the terminal error if the - /// connection can no longer send. - /// - /// Every send failure has to reach [`Self::end_connection`]: otherwise the - /// connection ends without `on_disconnect` running, because the run loops - /// treat a normal-disconnect error as a plain end of the loop. - /// - /// Note the deliberate two-step shape: `end_connection` takes the - /// `send_chan` lock, and `StdMutex` is not reentrant, so the lock has to be - /// released before calling it. - fn try_send(&self, msg: ws::v2::ClientMessage) -> crate::Result<()> { - let sent = { - let mut send_chan = self.send_chan.lock().unwrap(); - match send_chan.as_mut() { - Some(send_chan) => send_chan.unbounded_send(msg).is_ok(), - None => false, - } - }; - if sent { - Ok(()) - } else { - // `None`, not `Some(Disconnected)`: this is a requested or observed - // closure, and the WebSocket-close path reports a clean end to - // `on_disconnect` in the same way. - Err(self.end_connection(None)) - } - } - match lifecycle { ConnectionLifecycle::Connecting => { let callback_error = callback_error.unwrap_or_else(|| crate::Error::FailedToConnect { @@ -390,8 +362,13 @@ impl DbContextImpl { /// Apply all queued [`PendingMutation`]s. fn apply_pending_mutations(&self) -> crate::Result<()> { - while let Ok(Some(pending_mutation)) = get_lock_sync(&self.pending_mutations_recv).try_next() { - self.apply_mutation(pending_mutation)?; + while let Ok(Some(mutation)) = get_lock_sync(&self.pending_mutations_recv).try_next() { + match self.apply_mutation(mutation) { + Err(crate::Error::Disconnected) => { + return Err(self.end_connection(None)); + } + result => result?, + } } Ok(()) @@ -408,7 +385,13 @@ impl DbContextImpl { // Register the subscription, so we can handle related messages from the server. inner.subscriptions.register_subscription(query_set_id, handle.clone()); if let Some(msg) = handle.start() { - self.try_send(ws::v2::ClientMessage::Subscribe(msg))?; + self.send_chan + .lock() + .unwrap() + .as_mut() + .ok_or(crate::Error::Disconnected)? + .unbounded_send(ws::v2::ClientMessage::Subscribe(msg)) + .map_err(|_| crate::Error::Disconnected)?; } // else, the handle was already cancelled. } @@ -426,7 +409,13 @@ impl DbContextImpl { callback(&self.make_event_ctx(())); } PendingUnsubscribeResult::SendUnsubscribe(m) => { - self.try_send(ws::v2::ClientMessage::Unsubscribe(m))?; + self.send_chan + .lock() + .unwrap() + .as_mut() + .ok_or(crate::Error::Disconnected)? + .unbounded_send(ws::v2::ClientMessage::Unsubscribe(m)) + .map_err(|_| crate::Error::Disconnected)?; } } } @@ -453,7 +442,13 @@ impl DbContextImpl { request_id, flags, }); - self.try_send(msg)?; + self.send_chan + .lock() + .unwrap() + .as_mut() + .ok_or(crate::Error::Disconnected)? + .unbounded_send(msg) + .map_err(|_| crate::Error::Disconnected)?; } // Invoke a procedure: stash its callback, then send the `CallProcedure` WS message. @@ -476,7 +471,13 @@ impl DbContextImpl { request_id, flags: ws::v2::CallProcedureFlags::Default, }); - self.try_send(msg)?; + self.send_chan + .lock() + .unwrap() + .as_mut() + .ok_or(crate::Error::Disconnected)? + .unbounded_send(msg) + .map_err(|_| crate::Error::Disconnected)?; } // Disconnect: close the connection. @@ -671,10 +672,7 @@ impl DbContextImpl { std::thread::spawn(move || loop { match this.advance_one_message_blocking() { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => { - this.end_connection(None); - return; - } + Err(e) if error_is_normal_disconnect(&e) => return, Err(e) => panic!("{e:?}"), } }) @@ -690,10 +688,7 @@ impl DbContextImpl { loop { match this.advance_one_message_async().await { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => { - this.end_connection(None); - return; - } + Err(e) if error_is_normal_disconnect(&e) => return, Err(e) => panic!("{e:?}"), } } @@ -708,14 +703,7 @@ impl DbContextImpl { loop { match this.advance_one_message_async().await { Ok(()) => (), - Err(e) if error_is_normal_disconnect(&e) => { - // A queued mutation can observe the connection closure before the - // run loop sees the closed message channel (for example a reducer - // queued behind `disconnect()`). Route the terminal state through - // `end_connection` so `on_disconnect` runs exactly once either way. - this.end_connection(None); - return Ok(()); - } + Err(e) if error_is_normal_disconnect(&e) => return Ok(()), Err(e) => return Err(e), } }