From b05593f086d67f5d7fde28de7b63cc0cfde8723b Mon Sep 17 00:00:00 2001 From: Zakk Date: Mon, 26 Oct 2026 14:56:16 +0000 Subject: [PATCH] fix(api): follow a caller's abort after garbage collection Node's Request.clone() links the clone's signal to the original only through a weak reference, so after a garbage collection an aborted read or operation start kept waiting for its deadline. Reads now send the original request, which has no body to protect, and the retry loop for operation starts passes the caller's signal to send() explicitly. --- changes/fix-api-request-abort.md | 3 +++ src/api/client.test.ts | 14 ++++++++++++++ src/api/client.ts | 4 ++-- vitest.config.ts | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changes/fix-api-request-abort.md diff --git a/changes/fix-api-request-abort.md b/changes/fix-api-request-abort.md new file mode 100644 index 00000000..d05dbe9f --- /dev/null +++ b/changes/fix-api-request-abort.md @@ -0,0 +1,3 @@ +Fixed + +- A cancelled read or operation start no longer keeps waiting for its deadline after a garbage collection. diff --git a/src/api/client.test.ts b/src/api/client.test.ts index 3bab6c16..ad012d34 100644 --- a/src/api/client.test.ts +++ b/src/api/client.test.ts @@ -494,6 +494,20 @@ describe('native transport', () => { controller.abort(); await failure; }); + // Node's Request.clone() holds its abort link weakly; a clone sent after a collection must still follow the caller. + it.each([ + ['a read', (api: Api, signal: AbortSignal) => api.runtime(signal)], + ['an operation start', (api: Api, signal: AbortSignal) => api.refreshProvider('remote', signal)] + ])('keeps a caller abort after garbage collection for %s', async (_name, call) => { + vi.useFakeTimers(); + vi.stubGlobal('fetch', hanging()); + const controller = new AbortController(); + const failure = expect(call(createApi('https://honk.test'), controller.signal)).rejects.toMatchObject({name: 'AbortError'}); + await vi.advanceTimersByTimeAsync(0); + (globalThis as unknown as {gc: () => void}).gc(); + controller.abort(); + await failure; + }); it('releases the deadline once the response is read or the request fails', async () => { vi.useFakeTimers(); vi.stubGlobal( diff --git a/src/api/client.ts b/src/api/client.ts index 98d02cf4..e1acba9b 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -107,7 +107,7 @@ export function createApi(base: string, token?: string, clock: ServerClock = cre fetch: async (request: Request) => { if (request.method !== 'GET' || !expectsJson(request)) return exchange(request); try { - return await filled(request, await exchange(request.clone())); + return await filled(request, await exchange(request)); } catch (error) { if (!(error instanceof ApiError) || (error.code !== 'empty_response' && error.code !== 'network_error')) throw error; await pause(READ_RETRY_MS, request.signal); @@ -134,7 +134,7 @@ export function createApi(base: string, token?: string, clock: ServerClock = cre if (!retryable) return send(request, undefined, write); // A refusal with Retry-After is waited out a few times; the caller sees the last refusal after that. for (let refused = 0; ; refused++) { - const response = await send(request.clone(), undefined, write); + const response = await send(request.clone(), {signal: request.signal}, write); if ((response.status !== 503 && response.status !== 429) || refused >= MAX_REFUSALS) return response; const error = await responseError(response.clone()); if (!error.transient) return response; diff --git a/vitest.config.ts b/vitest.config.ts index e2a6bca5..1dd5a62c 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -9,6 +9,8 @@ export default defineConfig({ css: {include: [/src\/ui\/styles\/(?:palettes\/)?[^/]+\.css/]}, include: ['src/**/*.test.{ts,tsx}', 'mock/**/*.test.ts', 'tools/*.test.mjs'], setupFiles: ['src/i18n/setup.test-env.ts'], + // gc() for the API client tests that collect garbage. + execArgv: ['--expose-gc'], coverage: { provider: 'v8', reporter: ['text', 'lcov'],