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

+9
View File
@@ -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 */ }
+44
View File
@@ -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<void> } | 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");
});
+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) {