Skip to content
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ gemini-debug.log
evals/logs/

temp_agents/

# Local implementation plan (not for submission)
implementation_plan.md
42 changes: 41 additions & 1 deletion packages/core/src/utils/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('fetch utils', () => {
});

describe('fetchWithTimeout', () => {
it('should handle timeouts', async () => {
it('should throw FetchError with ETIMEDOUT on an internal timeout', async () => {
vi.mocked(global.fetch).mockImplementation(
(_input, init) =>
new Promise((_resolve, reject) => {
Expand All @@ -182,5 +182,45 @@ describe('fetch utils', () => {
'Request timed out after 50ms',
);
});

it('should throw an AbortError (not ETIMEDOUT) when the caller signal is aborted', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will time out or hang indefinitely. In fetch.test.ts, the mock implementation of global.fetch only listens for the abort event. However, the test calls controller.abort() before calling fetchWithTimeout. This means init.signal is already aborted by the time fetch is called, so the abort event never fires, leaving the mocked promise hanging forever. The mock needs to be updated to check init?.signal?.aborted synchronously.

vi.mocked(global.fetch).mockImplementation(
(_input, init) =>
new Promise((_resolve, reject) => {
const rejectWithAbortError = () => {
const error = new Error('The operation was aborted');
error.name = 'AbortError';
// @ts-expect-error - for mocking purposes
error.code = 'ABORT_ERR';
reject(error);
};

// Handle the case where the signal is already aborted before
// fetch is called (e.g. controller.abort() called synchronously).
if (init?.signal?.aborted) {
rejectWithAbortError();
return;
}

if (init?.signal) {
init.signal.addEventListener('abort', rejectWithAbortError, {
once: true,
});
}
}),
);

const controller = new AbortController();
// Abort the external signal before the request even starts
controller.abort();

const rejection = fetchWithTimeout('http://example.com', 10_000, {
signal: controller.signal,
});

await expect(rejection).rejects.toMatchObject({ name: 'AbortError' });
// Must NOT be classified as a timeout
await expect(rejection).rejects.not.toThrow('timed out');
});
});
});
8 changes: 8 additions & 0 deletions packages/core/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ export async function fetchWithTimeout(
return response;
} catch (error) {
if (isNodeError(error) && error.code === 'ABORT_ERR') {
// If the caller's own signal was already aborted, this is a user-initiated
// cancellation (e.g. Ctrl+C), not an internal timeout. Re-throw as a plain
// AbortError so the retry layer does NOT treat it as a retryable ETIMEDOUT.
if (options?.signal?.aborted) {
// Rethrow the original abort reason or the caught error to preserve
// the stack trace and any custom abort reason (e.g. from Ctrl+C).
throw options.signal.reason ?? error;
}
throw new FetchError(`Request timed out after ${timeout}ms`, 'ETIMEDOUT');
}
throw new FetchError(getErrorMessage(error), undefined, { cause: error });
Expand Down