fix(acceleration-relay): rebuild a self-closed tunnel on reconnect

An acceleration tunnel can tear itself down on its own: a socket error, a relay
OPEN_ERR/CLOSE frame, or an open timeout runs closeSockets(), which closes both
UDP sockets and leaves every later sendFrame() a silent no-op.

TSClient kept the adapter bound to that tunnel's now-dead local port and only
rebuilt the transport when adapter or client were null. An auto-reconnect
therefore dialed a port nobody reads, and because a closed tunnel reports
nothing, the session could never recover and never surfaced an error either.

- expose isClosed() on AccelerationRelayClient so callers can distinguish a live
  tunnel from one that tore itself down
- drop a stale tunnel together with its adapter and SDK client before connect()
  decides whether to rebuild the transport, detaching first so a late
  "disconnected" from the retired client cannot re-enter the reconnect path
- cover the reconnect guard with regression tests in ts-client.test.ts

Verified: tsc clean; ts-client.test.ts 35/35 pass (incl. the two new guards).
This commit is contained in:
LainHE committed 2026-10-15 21:36:31 +08:00
1 parent 62af82f253
commit 7977572362
3 files changed
+78

No files matched your search

+25
View File
@@ -127,6 +127,25 @@ export class TSClient extends EventEmitter {
this.identity = options.identity ?? generateIdentity(8);
}
/**
* Discards an acceleration tunnel that closed itself (socket error, relay
* OPEN_ERR/CLOSE, or open timeout) together with the adapter and SDK client
* bound to its now-dead local port, so the next connect() builds all three
* from scratch. A closed tunnel silently drops every frame, so reusing the
* adapter would make reconnection fail forever without an error.
*/
private dropStaleAccelerationTransport(): void {
if (!this.accelerationClient?.isClosed()) return;
const staleAdapter = this.adapter;
// Detach before teardown: every SDK listener bails when it no longer owns
// this.client, so a late "disconnected" from the stale transport cannot
// re-enter connect() through the reconnect path.
this.accelerationClient = null;
this.adapter = null;
this.client = null;
try { void staleAdapter?.disconnect().catch(() => undefined); } catch { /* already gone */ }
}
async connect(): Promise<void> {
this.connectionGeneration++;
this.inputState = { muted: false, accompaniment: false };
@@ -135,6 +154,12 @@ export class TSClient extends EventEmitter {
this.selfLeaveReasonId = null;
this.channelCapacities.clear();
this.channelPasswordProtection.clear();
// A relay tunnel that tore itself down (socket error, OPEN_ERR/CLOSE frame,
// open timeout) reports nothing and no-ops every send, while adapter and
// client still look reusable. Reconnecting through the dead local port would
// silently fail forever, so drop the whole stack and let the block below
// build a fresh tunnel plus adapter.
this.dropStaleAccelerationTransport();
if (!this.adapter || !this.client) {
let transportTarget = this.options.target;
if (this.options.acceleration && !this.accelerationClient) {