Repository navigation
Description
Summary
In packages/server/src/adapters/ws.ts, the run(async () => { ... }).catch(...) block that handles the subscription loop (~lines 328-424) does not call clientSubscriptions.delete(id) in its inner .catch() handler. Because clientSubscriptions.set(id, abortController) runs synchronously at ~line 425 — after run() is invoked but before its async body settles — any rejection of that async function leaves a stale entry in the Map permanently.
Reachable rejection path
The robust trigger is iterator cleanup: iteratorResource() wraps the subscription iterator with Symbol.asyncDispose, and that disposer awaits iterator.return?.(). If return() rejects during abort/cleanup (for example, an Observable teardown error inside a defer / from source), the subscription task rejects into the inner .catch() at line 408. The current handler aborts the controller and sends an error response, but does not remove id from clientSubscriptions.
Secondarily, the same stale-entry outcome applies to any synchronous exception raised after clientSubscriptions.set(id, abortController) but before the normal-path clientSubscriptions.delete(id) — for example a throw from response serialization/encoding or a throwing WebSocket test double. This claim does not rely on ordinary close-state ws.send() behavior (which is generally async-deferred and does not throw synchronously on CLOSING/CLOSED).
Severity
Low availability / connection-scoped resource leak and subscription-id lockout. Not a security issue. Stale AbortController entries accumulate in clientSubscriptions for the connection's lifetime; the same id cannot be re-used until the WebSocket closes and the Map is discarded.
Fix
Add clientSubscriptions.delete(id) as the first line of the .catch() handler at line 408, mirroring the cleanup already present on the happy path at line 407:
}).catch((cause) => {
clientSubscriptions.delete(id); // add this line
const error = getTRPCErrorFromUnknown(cause);
// ... existing error response logic
abortController.abort();
});Evidence
packages/server/src/adapters/ws.ts:clientSubscriptions.delete(id)only in the normal-exit path around line 407; the inner.catch(cause => ...)around lines 408-424 has nodelete;clientSubscriptions.set(id, abortController)runs synchronously around line 426.packages/server/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts:iteratorResource()usesSymbol.asyncDisposeand awaitsiterator.return?.()on cleanup; any rejection from thatreturn()call propagates out of the async function body and lands in the outer.catch().
PoC
The disposal-throw path is a minimal Node 20+ reproducer that vendors the ws.ts pattern verbatim:
class AbortController {
constructor() { this.signal = { onabort: null }; }
abort() { this.signal.onabort?.(); }
}
function iteratorResource(iterable) {
const iter = iterable[Symbol.asyncIterator]();
return {
next: () => iter.next(),
[Symbol.asyncDispose]: async () => { await iter.return?.(); },
};
}
function run(fn) { return fn(); }
async function reproduce() {
const clientSubscriptions = new Map();
const id = 'sub-1';
const abortController = new AbortController();
// Iterable whose iterator.return() rejects during await-using disposal:
const throwingIterable = {
[Symbol.asyncIterator]() {
return {
next: async () => ({ value: 99, done: false }),
return: async () => { throw new Error('iterator disposal failed'); },
};
},
};
run(async () => {
await using iterator = iteratorResource(throwingIterable);
abortController.abort();
// `await using` calls [asyncDispose] = return() on scope exit, which throws,
// propagating out of this async function body to run().catch() below.
}).catch((cause) => {
console.error(`[catch] run() rejected: ${cause.message}`);
abortController.abort();
// BUG: clientSubscriptions.delete(id) is missing here
});
clientSubscriptions.set(id, abortController);
await new Promise(resolve => setImmediate(resolve));
if (clientSubscriptions.size === 1) {
console.log(`[LEAK CONFIRMED] id="${id}" still in Map after iterator.return() throw`);
}
}
await reproduce();Expected output (current code): [LEAK CONFIRMED] id="sub-1" still in Map after iterator.return() throw.
After adding clientSubscriptions.delete(id) as the first line of .catch(), the LEAK CONFIRMED line disappears and Map size is 0.
Activity
fix(server): delete stale clientSubscriptions entry on subscription t…
fix(server): delete stale clientSubscriptions entry on subscription t…
fix(server): delete stale clientSubscriptions entry on subscription t…
fix(server): delete stale clientSubscriptions entry on subscription t…
github-actions commentedon Sep 13, 2026
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.
fix(client,server): wire AbortSignal in wsLink and delete stale clien…