Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(socket): gate emit on Connected so pre-handshake sends are not falsely acked\n\nSocketManager::emit guarded only on emit_tx being Some, which spawn_loop sets\nbefore the Socket.IO handshake. So an emit during Connecting returned {ok:true}\neven though ws_loop's drain_pending_emits silently discards the queued message\nif the connection then fails (#4355). The caller was told success for a dropped\nmessage.\n\nGate emit on status == Connected (reusing is_connected), returning the same\n"Not connected" error a pre-connect emit already returns. All production\ncallers already handle Err. Add a regression test proving emit errors while\nConnecting even with the channel present.\n\nFixes #6084\n

shanub58a7e070306
diff --git a/src/openhuman/platform/socket/manager.rs b/src/openhuman/platform/socket/manager.rs
index 44d102b47a..fc9864876c 100644
--- a/src/openhuman/platform/socket/manager.rs
+++ b/src/openhuman/platform/socket/manager.rs
@@ -172,7 +172,6 @@ impl SocketManager {
}
/// Check if the socket is currently connected.
- #[allow(dead_code)]
pub fn is_connected(&self) -> bool {
*self.shared.status.read() == ConnectionStatus::Connected
}
@@ -299,7 +298,19 @@ impl SocketManager {
}
/// Emit a Socket.IO event to the server.
+ ///
+ /// Gated on connection **readiness**, not merely on the emit channel
+ /// existing. `spawn_loop` installs `emit_tx` while the status is still
+ /// `Connecting` (before the Engine.IO / Socket.IO handshake completes), so a
+ /// channel-only guard would report success for a message that `ws_loop`'s
+ /// `drain_pending_emits` silently discards if the handshake then fails
+ /// (#4355). Callers must be told the truth, so an emit before the socket is
+ /// `Connected` returns the same `"Not connected"` error as an emit before
+ /// `connect` was ever called.
pub async fn emit(&self, event: &str, data: serde_json::Value) -> Result<(), String> {
+ if !self.is_connected() {
+ return Err("Not connected".to_string());
+ }
if let Some(ref tx) = *self.emit_tx.lock().await {
let msg = encode_sio_event(event, data, None)?;
tx.send(msg).map_err(|_| "Socket not connected".to_string())
diff --git a/src/openhuman/platform/socket/manager_tests.rs b/src/openhuman/platform/socket/manager_tests.rs
index 30aa6449e9..c0f98ecadf 100644
--- a/src/openhuman/platform/socket/manager_tests.rs
+++ b/src/openhuman/platform/socket/manager_tests.rs
@@ -56,6 +56,51 @@ async fn emit_without_connection_errors_without_panic() {
assert_eq!(err, "Not connected");
}
+#[tokio::test]
+async fn emit_while_connecting_errors_even_with_emit_channel_present() {
+ // Arrange: mirror `spawn_loop`'s pre-handshake state — the emit channel is
+ // installed (so the old channel-only guard would pass) but the socket has
+ // not finished the SIO handshake yet.
+ let mgr = SocketManager::new();
+ let (tx, mut rx) = mpsc::unbounded_channel::<String>();
+ *mgr.emit_tx.lock().await = Some(tx);
+ *mgr.shared.status.write() = ConnectionStatus::Connecting;
+
+ // Act
+ let err = mgr
+ .emit("test.event", json!({ "k": "v" }))
+ .await
+ .unwrap_err();
+
+ // Assert: the caller is told the truth, and nothing was queued onto a
+ // channel whose message `drain_pending_emits` would discard (#4355).
+ assert_eq!(err, "Not connected");
+ assert!(
+ rx.try_recv().is_err(),
+ "emit must not queue a message before the socket is Connected"
+ );
+}
+
+#[tokio::test]
+async fn emit_when_connected_queues_the_encoded_event() {
+ // Arrange
+ let mgr = SocketManager::new();
+ let (tx, mut rx) = mpsc::unbounded_channel::<String>();
+ *mgr.emit_tx.lock().await = Some(tx);
+ *mgr.shared.status.write() = ConnectionStatus::Connected;
+
+ // Act
+ mgr.emit("test.event", json!({ "k": "v" }))
+ .await
+ .expect("emit must succeed once the socket is Connected");
+
+ // Assert: the encoded Socket.IO event lands on the outbound channel.
+ let queued = rx
+ .try_recv()
+ .expect("expected the emitted event to be queued");
+ assert_eq!(queued, r#"42["test.event",{"k":"v"}]"#);
+}
+
#[tokio::test]
async fn emit_with_ack_without_connection_errors_without_waiting() {
let mgr = SocketManager::new();