Skip to content

fix: remove abort event listener on successful fetch completion#1921

Open
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/fetch-abort-listener-leak-1811
Open

fix: remove abort event listener on successful fetch completion#1921
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:fix/fetch-abort-listener-leak-1811

Conversation

@Oxygen56

@Oxygen56 Oxygen56 commented Jun 5, 2026

Copy link
Copy Markdown

Cleans up the abort event listener in fetchWithTimeout on successful completion to prevent Deno process exit hang.

Problem

fetchWithTimeout adds an abort event listener on the caller's AbortSignal to forward abort events to the internal controller. On successful completion (response received before timeout), the timeout is cleared via clearTimeout, but the abort event listener is never removed.

In Deno, AbortSignal.timeout() refs the underlying timer when it has event listeners. The orphaned listener keeps the timer ref'd for the full timeout duration, preventing the process from exiting.

Fix

Add signal.removeEventListener('abort', abort) in the finally block alongside the existing clearTimeout(timeout). This ensures the event listener is always cleaned up on successful completion while preserving the existing { once: true } behavior for the abort path.

Testing

  • The { once: true } option on addEventListener continues to handle the timeout-abort path (self-removal)
  • The new removeEventListener call handles the success path (explicit cleanup)
  • No behavior change for the error path — finally block runs regardless

Fixes #1811

fetchWithTimeout was not cleaning up the abort event listener
on successful completion, preventing Deno process exit.

Fixes openai#1811

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Oxygen56 Oxygen56 requested a review from a team as a code owner June 5, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetchWithTimeout does not remove abort event listener on successful completion, preventing process exit on Deno

1 participant