fix(qconnect): subscribe transport events before the session-loop spawn (slice 5)

Slice 5 moved the transport subscription INTO the spawned run_session_loop
(self.subscribe_transport_events()), which races the WS handshake:
NativeWsTransport::connect spawns the WS loop and returns WITHOUT awaiting it,
and tokio broadcast has no replay -- so the initial Connected / Subscribed /
SessionEstablished / SESSION_STATE events could be sent to zero receivers and
silently dropped before the spawned task is first polled (a lost
SessionEstablished leaves the UI stuck Connecting; a lost SESSION_STATE skips
the deferred renderer join). The 42 wire tests miss it: they subscribe before
connect against a non-self-driving InMemoryWsTransport.

Restore the prior guarantee -- subscribe synchronously in connect() right after
app.connect() returns and before the spawn / any further await, and pass the
broadcast::Receiver into run_session_loop. Found by adversarial review.

qconnect-app 38 green, src-tauri qconnect 42 green.

cc
This commit is contained in:
vicrodh committed 2026-06-02 16:32:08 -06:00
1 parent a07bb36327
commit 6b26ea0e5e
2 files changed
+24 -3

No files matched your search

+11 -2
View File
@@ -1668,8 +1668,17 @@ where
/// prior inline Tauri loop; the adapter-coupled seams (lifecycle gating,
/// teardown, bootstrap, renderer-join, raw error) go through `host` so both
/// frontends share this control flow.
pub async fn run_session_loop(&self, host: Arc<dyn SessionLoopHost>, idle_retry_active: bool) {
let mut transport_rx = self.subscribe_transport_events();
pub async fn run_session_loop(
&self,
host: Arc<dyn SessionLoopHost>,
mut transport_rx: tokio::sync::broadcast::Receiver<TransportEvent>,
idle_retry_active: bool,
) {
// NOTE: `transport_rx` is subscribed by the adapter SYNCHRONOUSLY before
// this task is spawned (and before any further await), so the receiver is
// live before the WS handshake can emit its initial events. tokio
// broadcast has no replay — subscribing here, inside the spawned task,
// would race those events and silently drop them.
log::info!("[QConnect/EventLoop] Started listening for transport events");
let mut renderer_joined = false;
let mut has_disconnected = false;