fix: remove abort listener from user signal after fetch completes#937
Open
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Open
fix: remove abort listener from user signal after fetch completes#937MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Conversation
In fetchWithTimeout(), when the user provides an AbortSignal, an abort
event listener is added to forward abort events to the internal
controller. However, this listener is never removed after the fetch
completes. This causes a memory leak when users pass long-lived
AbortSignal instances (e.g., from AbortController instances that outlive
individual requests), as the listener retains a reference to the
internal controller and prevents garbage collection.
The existing code comments (lines 799-804) explicitly discuss avoiding
memory leaks from closures by using _makeAbort() instead of arrow
functions, but the listener removal in the finally block was missing.
This fix adds signal.removeEventListener('abort', abort) to the finally
block, ensuring the listener is cleaned up regardless of whether the
fetch succeeds, fails, or times out.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fetchWithTimeout(): When the user provides anAbortSignal, an abort event listener is added to forward abort events to the internal controller. However, this listener is never removed after the fetch completes. This causes a memory leak when users pass long-livedAbortSignalinstances, as the listener retains a reference to the internalAbortControllerand prevents garbage collection.Details
In
src/client.ts, thefetchWithTimeout()method adds an event listener on line 806:The existing code comments (lines 799-804) explicitly discuss the importance of avoiding memory leaks from closures by using
_makeAbort()instead of arrow functions. However, while thetimeoutis correctly cleaned up in thefinallyblock, the abort listener on the user's signal is never removed.This means if a user passes a long-lived
AbortSignal(e.g., from anAbortControllerthat outlives individual requests), each request leaves behind a dangling listener that keeps the internalAbortControlleralive in memory.The fix adds
signal.removeEventListener('abort', abort)to thefinallyblock, ensuring the listener is cleaned up regardless of whether the fetch succeeds, fails, or times out. Note that while{ once: true }is used on theaddEventListenercall, this only removes the listener if it actually fires — it does not help when the request completes normally without the signal being aborted.Test plan
finallyblock, matching the pattern already used forclearTimeout(timeout)removeEventListeneris safe to call even if the listener was already removed (no-op)🤖 Generated with Claude Code