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

+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");
});