fix(app): merge abort signals in apiFetch#2
Merged
GeminiLight merged 1 commit intoGeminiLight:mainfrom Mar 22, 2026
Merged
Conversation
Ensure apiFetch honors both timeout cancellation and caller-provided AbortSignal.\n\nPreviously, providing an external signal could bypass the internal timeout signal.\nThis patch bridges the external signal into a shared AbortController so either source can abort the request.\n\nAlso cleans up the external abort event listener in finally to avoid leaks.
There was a problem hiding this comment.
Pull request overview
Updates the shared apiFetch helper to correctly support both request timeouts and caller-provided cancellation signals, ensuring the timeout isn’t accidentally disabled when an external AbortSignal is supplied.
Changes:
- Creates an internal
AbortControllerwhen a timeout and/or external signal is present. - Bridges a caller-provided
AbortSignalinto the internal controller so external aborts cancel the underlyingfetch. - Ensures timers and abort listeners are cleaned up in
finally.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| let removeExternalAbortListener: (() => void) | undefined; | ||
|
|
||
| if (timeout > 0) { | ||
| if (timeout > 0 || externalSignal) { |
Comment on lines
+42
to
+45
| if (externalSignal.aborted) { | ||
| controller.abort(); | ||
| } else { | ||
| const onAbort = () => controller?.abort(); |
Comment on lines
25
to
33
| export async function apiFetch<T>(url: string, opts: ApiFetchOptions = {}): Promise<T> { | ||
| const { timeout = 30_000, signal: externalSignal, ...fetchOpts } = opts; | ||
|
|
||
| let controller: AbortController | undefined; | ||
| let timeoutId: ReturnType<typeof setTimeout> | undefined; | ||
| let removeExternalAbortListener: (() => void) | undefined; | ||
|
|
||
| if (timeout > 0) { | ||
| if (timeout > 0 || externalSignal) { | ||
| controller = new AbortController(); |
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.
Ensure apiFetch honors both timeout cancellation and caller-provided AbortSignal.\n\nPreviously, providing an external signal could bypass the internal timeout signal.\nThis patch bridges the external signal into a shared AbortController so either source can abort the request.\n\nAlso cleans up the external abort event listener in finally to avoid leaks.