diff --git a/crates/voelin-core/src/link/tests.rs b/crates/voelin-core/src/link/tests.rs index 751703f..e565eb6 100644 --- a/crates/voelin-core/src/link/tests.rs +++ b/crates/voelin-core/src/link/tests.rs @@ -1474,3 +1474,66 @@ async fn a_voice_packet_costs_microseconds_through_both_adapters() { assert!(uplink < Duration::from_micros(500), "{uplink:?}"); assert!(downlink < Duration::from_micros(500), "{downlink:?}"); } + +/// What the stream task reports before the engine has announced the +/// connection waits for it (`ts_session/driver.rs`, `Held`): the engine acts +/// on a list of streams as soon as it sees one, and has to know it has voice +/// by then, as it did when the stream task was not behind a session. +#[tokio::test] +async fn the_stream_tasks_reports_wait_for_the_engine_to_announce_the_connection() { + let mut rig = Rig::new(everything()).await; + let listed = || Event::StreamsChanged { session: 0, streams: Vec::new() }; + rig.inner.ctx.events.send(listed()).unwrap(); + rig.inner.ctx.events.send(Event::Error { session: 0, message: "second".into() }).unwrap(); + tokio::time::sleep(Duration::from_millis(100)).await; + assert!( + !on_the_bus(&mut rig), + "the list was passed on before the engine's first stream command" + ); + + // The engine's first stream command comes after it announced the + // connection; the reports follow it, in their order. + rig.give(LinkCmd::Stream(StreamCmd::Directory(Vec::new()))); + assert!(matches!(rig.inner_cmd().await, LinkCmd::Stream(StreamCmd::Directory(_)))); + rig.bus(|e| matches!(e, Event::StreamsChanged { .. }).then_some(())).await; + rig.bus(|e| matches!(e, Event::Error { message, .. } if message == "second").then_some(())) + .await; + + // Afterwards they go straight through. + rig.inner.ctx.events.send(listed()).unwrap(); + rig.bus(|e| matches!(e, Event::StreamsChanged { .. }).then_some(())).await; +} + +/// Nobody announces the connection to a session used without an engine: its +/// stream reports are held for a while only. +#[tokio::test] +async fn the_stream_tasks_reports_are_not_held_for_ever() { + let mut rig = Rig::new(everything()).await; + rig.inner.ctx.events.send(Event::StreamsChanged { session: 0, streams: Vec::new() }).unwrap(); + tokio::time::sleep(Duration::from_millis(50)).await; + assert!(!on_the_bus(&mut rig), "the list was not held at all"); + rig.bus(|e| matches!(e, Event::StreamsChanged { .. }).then_some(())).await; +} + +/// A link without streams has no stream task to hold back: what it puts on +/// the bus goes straight through. +#[tokio::test] +async fn a_link_without_streams_is_not_held_back() { + let mut rig = Rig::new(Capabilities::default()).await; + rig.inner.ctx.events.send(Event::Error { session: 0, message: "now".into() }).unwrap(); + tokio::time::timeout( + Duration::from_millis(250), + rig.bus(|e| matches!(e, Event::Error { message, .. } if message == "now").then_some(())), + ) + .await + .expect("the report went through at once"); +} + +/// Whether a list of streams is on the engine's bus now. +fn on_the_bus(rig: &mut Rig) -> bool { + let mut found = false; + while let Ok(event) = rig.bus.try_recv() { + found |= matches!(event, Event::StreamsChanged { .. }); + } + found +} diff --git a/crates/voelin-core/src/ts_session/driver.rs b/crates/voelin-core/src/ts_session/driver.rs index 8705765..d086936 100644 --- a/crates/voelin-core/src/ts_session/driver.rs +++ b/crates/voelin-core/src/ts_session/driver.rs @@ -43,6 +43,13 @@ const WINDOW: usize = 50; const SYNC_CHUNK: usize = 256; /// Most CHAT scopes at once (`Limits.max_chat_subscriptions`). pub(super) const MAX_CHAT_SUBSCRIPTIONS: u32 = 16; +/// How long what the stream task reports waits for the engine to have +/// announced the connection (see [`Held`]). +#[cfg(not(test))] +const ANNOUNCE_WAIT: Duration = Duration::from_secs(3); +/// (Short in tests, which wait for it.) +#[cfg(test)] +const ANNOUNCE_WAIT: Duration = Duration::from_millis(400); /// A request that waits on this task's answer. pub(super) type Reply = oneshot::Sender>; @@ -108,6 +115,22 @@ struct Pending { deadline: Instant, } +/// What the stream task puts on the engine's bus before the engine has +/// announced the connection. +/// +/// The stream task starts with the link and lists the streams it finds as +/// soon as the server tells it; the engine learns of the connection only +/// after the connect sequence of the session (a few requests through this +/// task), and a list that arrives first would be acted on before the engine +/// knows it has voice. A link that was not behind a session delivered the two +/// in the other order, so the reports wait: the engine's first stream command +/// (sent when it handles the connection, after it has announced it) releases +/// them, or the time they may wait ([`ANNOUNCE_WAIT`]) does. +struct Held { + events: Vec, + until: Instant, +} + /// The state of the task. pub(super) struct Driver { cfg: Config, @@ -117,6 +140,8 @@ pub(super) struct Driver { bus: broadcast::Receiver, bus_open: bool, engine_bus: broadcast::Sender, + /// The stream task's reports, until the engine has announced the connection. + held: Option, pushes: mpsc::Sender, media: Arc, port: Option>, @@ -237,6 +262,10 @@ impl Driver { if let Some(key) = Key::of(&chat) { server_layer.insert(key, chat); } + // Only a link with streams has a stream task to hold back. + let held = port + .is_some() + .then(|| Held { events: Vec::new(), until: Instant::now() + ANNOUNCE_WAIT }); let mut driver = Self { cfg, inner, @@ -245,6 +274,7 @@ impl Driver { bus, bus_open: true, engine_bus, + held, pushes, media, port, @@ -381,7 +411,11 @@ impl Driver { Cmd::Read { request, reply } => self.read(request, reply), Cmd::Write { request, reply } => self.write(request, reply), Cmd::Audio(voice) => self.send_inner(LinkCmd::Audio(voice)), - Cmd::Stream(cmd) => self.send_inner(LinkCmd::Stream(cmd)), + Cmd::Stream(cmd) => { + // The engine handled the connection before it sent this. + self.announced(); + self.send_inner(LinkCmd::Stream(cmd)); + } Cmd::Close { code, reason } => self.close(code, reason), } } @@ -813,6 +847,16 @@ impl Driver { // ---- answers from the link ------------------------------------------- + /// The engine has announced the connection: pass on what the stream task + /// reported meanwhile, in its order. + fn announced(&mut self) { + if let Some(held) = self.held.take() { + for event in held.events { + let _ = self.engine_bus.send(event); + } + } + } + fn bus_event(&mut self, event: Event) { match event { Event::FileList { request, channel, path, result, .. } => { @@ -869,9 +913,12 @@ impl Driver { } } // What the stream task says (its own bus events) goes to the engine. - other => { - let _ = self.engine_bus.send(other); - } + other => match &mut self.held { + Some(held) => held.events.push(other), + None => { + let _ = self.engine_bus.send(other); + } + }, } } @@ -941,6 +988,11 @@ impl Driver { fn tick(&mut self) { let now = Instant::now(); + // Nobody announced the connection (a session used without an engine): + // the reports are not held for ever. + if self.held.as_ref().is_some_and(|held| held.until <= now) { + self.announced(); + } let expired: Vec = self.pending.iter().filter(|(_, p)| p.deadline <= now).map(|(id, _)| *id).collect(); for id in expired { diff --git a/docs/session-seam.md b/docs/session-seam.md index 2fb0086..7dbcf97 100644 --- a/docs/session-seam.md +++ b/docs/session-seam.md @@ -883,6 +883,25 @@ public item was removed. Where the code differs from the text above: order. A packet from a slot the replica does not know yet fetches the queued pushes first and tries again, so a talker's first packet is not lost either way. +- **The stream task's reports wait for the engine's announcement.** The + TeamSpeak 6 stream task puts `Event::StreamsChanged` and the like on the + engine's bus itself. Behind a session the engine learns of the connection + only after the connect sequence (a few requests through the driver), while + the stream task lists the server's streams as soon as the server tells it; + a client that acts on the first list (`voelinctl gateway --engine + --watch-streamer`, the `directory_check` of `scripts/it-smoke.sh`) sent + `WatchStream` before the engine knew it had voice, and failed with `not + connected with voice` in half of the runs (3 of 6; the baseline binary + passed all 4 runs the server's clone limit did not end first, the two + alternating on one TeamSpeak 6 server). The + driver of `ts_session` therefore holds the stream task's bus events back + (`Held` in `ts_session/driver.rs`; only for a link with streams, and only + the events that are no answer to a request) until the engine's first + stream command, which `session.rs` sends in its handling of `Connected` + right after it has emitted the state, or for 3 seconds. Three unit tests + pin it (`link/tests.rs`: held until the first stream command, not held for + ever, not held for a link without streams); the same probe passes 8 of 8 + with it. - **CHAT scopes are re-subscribed in full.** The replica drops a scope's cursor and entities when it is unsubscribed and refuses a delta sync without a baseline, so SessionLink does not keep cursors for a delta