diff --git a/src/server/acceleration-relay.ts b/src/server/acceleration-relay.ts index 5303cf6..7bef30f 100644 --- a/src/server/acceleration-relay.ts +++ b/src/server/acceleration-relay.ts @@ -40,6 +40,14 @@ export interface AccelerationRelayClient { readonly localHost: string; readonly localPort: number; readonly sessionId: number; + /** + * True once the tunnel has torn itself down, whether by {@link close} or on + * its own after a socket error, a relay OPEN_ERR/CLOSE frame, or an open + * timeout. Callers that route a transport through this tunnel must watch this + * flag: after it flips, every sendFrame() call is a silent no-op and both UDP + * sockets are closed, so the tunnel can never recover on its own. + */ + isClosed(): boolean; close(): void; } @@ -150,6 +158,7 @@ export async function createAccelerationRelayClient( localHost: "127.0.0.1", localPort: typeof address === "object" ? address.port : 0, sessionId, + isClosed: (): boolean => closed, close(): void { if (!closed && ready) { try { sendFrame(FrameType.CLOSE); } catch { /* best effort */ } diff --git a/src/server/ts-client.test.ts b/src/server/ts-client.test.ts index 1761b2f..a1a0986 100644 --- a/src/server/ts-client.test.ts +++ b/src/server/ts-client.test.ts @@ -410,3 +410,47 @@ for (const [name, payload] of retiredEvents) { assert.equal(observed.length, 1); }); } + +interface RelayFixtureState { + accelerationClient: { isClosed(): boolean; close(): void } | null; + adapter: { disconnect(): Promise } | null; + client: SdkStub | null; + dropStaleAccelerationTransport(): void; +} + +function relayFixture(tunnelClosed: boolean) { + const client = new TSClient({ + target: { host: "voice.example.invalid", port: 9987 }, + nickname: "Test", + acceleration: { relayHost: "127.0.0.1", relayPort: 39087, token: "test-acceleration-relay-token" }, + }, pino({ enabled: false })); + let disconnectCalls = 0; + let closeCalls = 0; + const state = client as unknown as RelayFixtureState; + state.accelerationClient = { + isClosed: () => tunnelClosed, + close: () => { closeCalls++; }, + }; + state.adapter = { disconnect: async () => { disconnectCalls++; } }; + state.client = new SdkStub(); + return { client, state, counts: () => ({ disconnectCalls, closeCalls }) }; +} + +test("a self-closed relay tunnel is dropped with its adapter so connect() rebuilds both", () => { + // Regression: reconnecting reused the adapter bound to the dead tunnel's local + // port. Every frame the tunnel sent was a silent no-op, so auto-reconnect + // could never recover and nothing reported an error. + const f = relayFixture(true); + f.state.dropStaleAccelerationTransport(); + assert.equal(f.state.accelerationClient, null, "the dead tunnel must not be reused"); + assert.equal(f.state.adapter, null, "the adapter bound to the dead local port must be dropped"); + assert.equal(f.state.client, null, "the SDK client for the dead transport must be dropped"); +}); + +test("a live relay tunnel survives the reconnect guard untouched", () => { + const f = relayFixture(false); + f.state.dropStaleAccelerationTransport(); + assert.notEqual(f.state.accelerationClient, null, "an open tunnel stays usable"); + assert.notEqual(f.state.adapter, null, "an open tunnel's adapter stays usable"); + assert.notEqual(f.state.client, null, "an open tunnel's SDK client stays usable"); +}); diff --git a/src/server/ts-client.ts b/src/server/ts-client.ts index 3829482..a3234ea 100644 --- a/src/server/ts-client.ts +++ b/src/server/ts-client.ts @@ -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 { 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) {