Files
triggerdotdev--trigger.dev/apps/webapp/app/services/httpAsyncStorage.server.ts
Eric Allam 02d2334c8a fix(webapp): fix Redis connection leak in realtime streams and broken abort signal propagation (#3399)
Pool Redis connections for non-blocking ops (ingestData, appendPart,
getLastChunkIndex)
using a shared singleton instead of new Redis() per request. Use
redis.disconnect()
for immediate teardown in streamResponse cleanup. Add 15s inactivity
timeout fallback.

Fix broken request.signal in Remix/Express by wiring Express
res.on('close') to an
AbortController via httpAsyncStorage. All SSE/streaming routes now use
getRequestAbortSignal() which fires reliably on client disconnect,
bypassing the
Node.js undici GC bug (nodejs/node#55428) that severs the signal chain.
2026-04-16 15:15:10 +01:00

34 lines
1.1 KiB
TypeScript

import { AsyncLocalStorage } from "node:async_hooks";
export type HttpLocalStorage = {
requestId: string;
path: string;
host: string;
method: string;
abortController: AbortController;
};
const httpLocalStorage = new AsyncLocalStorage<HttpLocalStorage>();
export type RunWithHttpContextFunction = <T>(context: HttpLocalStorage, fn: () => T) => T;
export function runWithHttpContext<T>(context: HttpLocalStorage, fn: () => T): T {
return httpLocalStorage.run(context, fn);
}
export function getHttpContext(): HttpLocalStorage | undefined {
return httpLocalStorage.getStore();
}
// Fallback signal that is never aborted, safe for tests and non-Express contexts.
const neverAbortedSignal = new AbortController().signal;
/**
* Returns an AbortSignal wired to the Express response's "close" event.
* This bypasses the broken request.signal chain in @remix-run/express
* (caused by Node.js undici GC bug nodejs/node#55428).
*/
export function getRequestAbortSignal(): AbortSignal {
return httpLocalStorage.getStore()?.abortController.signal ?? neverAbortedSignal;
}