perf: use .abort.bind() instead of arrow closures to prevent memory leaks (#7174)

This commit is contained in:
Marcus Castro
2026-02-06 20:30:29 -03:00
committed by Peter Steinberger
parent d637a26350
commit d9c582627c
12 changed files with 17 additions and 17 deletions
+1 -1
View File
@@ -185,7 +185,7 @@ async function withTimeout<T>(
fn: (signal: AbortSignal) => Promise<T>, fn: (signal: AbortSignal) => Promise<T>,
): Promise<T> { ): Promise<T> {
const controller = new AbortController(); const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs); const timer = setTimeout(controller.abort.bind(controller), timeoutMs);
try { try {
return await fn(controller.signal); return await fn(controller.signal);
} finally { } finally {
+1 -1
View File
@@ -36,7 +36,7 @@ function combineAbortSignals(a?: AbortSignal, b?: AbortSignal): AbortSignal | un
} }
const controller = new AbortController(); const controller = new AbortController();
const onAbort = () => controller.abort(); const onAbort = controller.abort.bind(controller);
a?.addEventListener("abort", onAbort, { once: true }); a?.addEventListener("abort", onAbort, { once: true });
b?.addEventListener("abort", onAbort, { once: true }); b?.addEventListener("abort", onAbort, { once: true });
return controller.signal; return controller.signal;
+1 -1
View File
@@ -24,7 +24,7 @@ async function waitForSandboxCdp(params: { cdpPort: number; timeoutMs: number })
while (Date.now() < deadline) { while (Date.now() < deadline) {
try { try {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), 1000); const t = setTimeout(ctrl.abort.bind(ctrl), 1000);
try { try {
const res = await fetch(url, { signal: ctrl.signal }); const res = await fetch(url, { signal: ctrl.signal });
if (res.ok) { if (res.ok) {
+1 -1
View File
@@ -65,7 +65,7 @@ export function withTimeout(signal: AbortSignal | undefined, timeoutMs: number):
return signal ?? new AbortController().signal; return signal ?? new AbortController().signal;
} }
const controller = new AbortController(); const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs); const timer = setTimeout(controller.abort.bind(controller), timeoutMs);
if (signal) { if (signal) {
signal.addEventListener( signal.addEventListener(
"abort", "abort",
+2 -2
View File
@@ -114,7 +114,7 @@ function createCdpSender(ws: WebSocket) {
export async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit): Promise<T> { export async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit): Promise<T> {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try { try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {}); const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal }); const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
@@ -129,7 +129,7 @@ export async function fetchJson<T>(url: string, timeoutMs = 1500, init?: Request
export async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promise<void> { export async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promise<void> {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try { try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {}); const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal }); const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
+1 -1
View File
@@ -80,7 +80,7 @@ type ChromeVersion = {
async function fetchChromeVersion(cdpUrl: string, timeoutMs = 500): Promise<ChromeVersion | null> { async function fetchChromeVersion(cdpUrl: string, timeoutMs = 500): Promise<ChromeVersion | null> {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try { try {
const versionUrl = appendCdpPath(cdpUrl, "/json/version"); const versionUrl = appendCdpPath(cdpUrl, "/json/version");
const res = await fetch(versionUrl, { const res = await fetch(versionUrl, {
+2 -2
View File
@@ -51,7 +51,7 @@ function normalizeWsUrl(raw: string | undefined, cdpBaseUrl: string): string | u
async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit): Promise<T> { async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit): Promise<T> {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try { try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {}); const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal }); const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
@@ -66,7 +66,7 @@ async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit):
async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promise<void> { async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promise<void> {
const ctrl = new AbortController(); const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try { try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {}); const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal }); const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
+1 -1
View File
@@ -42,7 +42,7 @@ export function wrapFetchWithAbortSignal(fetchImpl: typeof fetch): typeof fetch
return fetchImpl(input, patchedInit); return fetchImpl(input, patchedInit);
} }
const controller = new AbortController(); const controller = new AbortController();
const onAbort = () => controller.abort(); const onAbort = controller.abort.bind(controller);
if (signal.aborted) { if (signal.aborted) {
controller.abort(); controller.abort();
} else { } else {
+2 -2
View File
@@ -50,8 +50,8 @@ function buildAbortSignal(params: { timeoutMs?: number; signal?: AbortSignal }):
} }
const controller = new AbortController(); const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs); const timeoutId = setTimeout(controller.abort.bind(controller), timeoutMs);
const onAbort = () => controller.abort(); const onAbort = controller.abort.bind(controller);
if (signal) { if (signal) {
if (signal.aborted) { if (signal.aborted) {
controller.abort(); controller.abort();
+1 -1
View File
@@ -5,7 +5,7 @@ export async function fetchJson(
fetchFn: typeof fetch, fetchFn: typeof fetch,
): Promise<Response> { ): Promise<Response> {
const controller = new AbortController(); const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs); const timer = setTimeout(controller.abort.bind(controller), timeoutMs);
try { try {
return await fetchFn(url, { ...init, signal: controller.signal }); return await fetchFn(url, { ...init, signal: controller.signal });
} finally { } finally {
+3 -3
View File
@@ -937,7 +937,7 @@ async function summarizeText(params: {
try { try {
const controller = new AbortController(); const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs); const timeout = setTimeout(controller.abort.bind(controller), timeoutMs);
try { try {
const res = await completeSimple( const res = await completeSimple(
@@ -1038,7 +1038,7 @@ async function elevenLabsTTS(params: {
const normalizedSeed = normalizeSeed(seed); const normalizedSeed = normalizeSeed(seed);
const controller = new AbortController(); const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs); const timeout = setTimeout(controller.abort.bind(controller), timeoutMs);
try { try {
const url = new URL(`${normalizeElevenLabsBaseUrl(baseUrl)}/v1/text-to-speech/${voiceId}`); const url = new URL(`${normalizeElevenLabsBaseUrl(baseUrl)}/v1/text-to-speech/${voiceId}`);
@@ -1098,7 +1098,7 @@ async function openaiTTS(params: {
} }
const controller = new AbortController(); const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs); const timeout = setTimeout(controller.abort.bind(controller), timeoutMs);
try { try {
const response = await fetch(`${getOpenAITtsBaseUrl()}/audio/speech`, { const response = await fetch(`${getOpenAITtsBaseUrl()}/audio/speech`, {
+1 -1
View File
@@ -15,7 +15,7 @@ export async function fetchWithTimeout(
fetchFn: typeof fetch = fetch, fetchFn: typeof fetch = fetch,
): Promise<Response> { ): Promise<Response> {
const controller = new AbortController(); const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), Math.max(1, timeoutMs)); const timer = setTimeout(controller.abort.bind(controller), Math.max(1, timeoutMs));
try { try {
return await fetchFn(url, { ...init, signal: controller.signal }); return await fetchFn(url, { ...init, signal: controller.signal });
} finally { } finally {