fix: recover from silent network drops on both sides of the gateway

Measured with relays that blackhole traffic:

- Gateway to TS server: tsclientlib reconnects on its own after a timeout or
  server restart, but the connector ignored that, so the browser got no notice
  and the new session stayed unsubscribed - everyone but us vanished from the
  tree. The connector now emits "reconnecting"/"reconnected", resubscribes,
  and pauses the server-variable poll meanwhile; the page logs both.
- Browser to gateway: a network that dies silently never closed the socket on
  the page, which kept showing "connected" indefinitely. The gateway now sends
  an app-level keepalive every 20 s (announced as "keepalive" in hello), and
  a socket quiet for 50 s is handled as dropped and reconnected.
- Coming back online skips any remaining reconnect backoff.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5.5 committed 2026-10-03 01:37:13 +00:00
1 parent 23e61c86b3
commit 12e482b223
8 files changed
+81 -4

No files matched your search

+21 -1
View File
@@ -293,6 +293,12 @@ enum Event {
Talkers { clients: Vec<u16> },
#[serde(rename = "disconnected")]
Disconnected { reason: String },
/// Lost the server (timeout, server restart); tsclientlib is reconnecting
/// on its own. Followed by "reconnected" once it is back.
#[serde(rename = "reconnecting")]
Reconnecting { reason: String },
#[serde(rename = "reconnected")]
Reconnected,
#[serde(rename = "error")]
Error { message: String },
/// A "switch" to a password-protected channel was rejected because no (or
@@ -1060,6 +1066,7 @@ async fn run(args: Args) -> Result<()> {
// TeaSpeak may reveal hidden channels after a group/permission change. Debounce
// a channelsubscribeall so we pick up any channels the server only exposes then.
let mut resubscribe_at: Option<tokio::time::Instant> = None;
let mut ts_reconnecting = false;
enum LoopOutcome {
StdinLine(std::io::Result<Option<String>>),
@@ -2437,6 +2444,13 @@ async fn run(args: Args) -> Result<()> {
},
LoopOutcome::ConEvent(ev) => match ev {
Some(Ok(StreamItem::BookEvents(events))) => {
if ts_reconnecting {
// The new session starts unsubscribed, which would leave
// everyone but us invisible.
ts_reconnecting = false;
resubscribe_at = Some(tokio::time::Instant::now());
emit(&Event::Reconnected);
}
let own_id = con.get_state()?.own_client;
for event in &events {
if let BookEvent::Message { target, invoker, message } = event {
@@ -3008,6 +3022,10 @@ async fn run(args: Args) -> Result<()> {
emit(&Event::Error { message: format!("File transfer failed: {e}") });
}
}
Some(Ok(StreamItem::DisconnectedTemporarily(reason))) => {
ts_reconnecting = true;
emit(&Event::Reconnecting { reason: format!("{reason:?}") });
}
Some(Ok(_)) => {}
Some(Err(e)) => {
emit(&Event::Disconnected { reason: e.to_string() });
@@ -3029,7 +3047,9 @@ async fn run(args: Args) -> Result<()> {
}
}
LoopOutcome::ServerVarsRefresh => {
send_server_get_variables(&mut con);
if !ts_reconnecting {
send_server_get_variables(&mut con);
}
}
LoopOutcome::AudioTick => {
if !audio_handler.get_queues().is_empty() {