mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-05 08:00:59 +00:00
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.
This commit is contained in:
1 parent
c50fcac366
commit
b05593f086
4 files changed
+21
-2
No files matched your search
@@ -0,0 +1,3 @@
|
||||
Fixed
|
||||
|
||||
- A cancelled read or operation start no longer keeps waiting for its deadline after a garbage collection.
|
||||
@@ -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(
|
||||
|
||||
+2
-2
@@ -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;
|
||||
|
||||
@@ -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'],
|
||||
|
||||
Reference in new issue
Block a user