fix: abort generateText on webSearch timeout#306
Merged
Conversation
When webSearch times out after 60s, the underlying generateText() call was left running in the background indefinitely — leaking connections and memory. Retries compounded the problem since each retry left behind another orphaned request. Fix: - Pass AbortController signal to generateText so the HTTP request is actually cancelled when the timeout fires. - Add onTimeout callback to withTimeout for clean post-rejection cleanup. Reject fires first so abort can never race the timeout error; callback is wrapped in try/catch so cleanup failures cannot break timeout semantics. Closes JUNIOR-1H Co-Authored-By: Claude (anthropic/claude-opus-4.6) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Problem
JUNIOR-1H —
Error: webSearch timed out(15 events, 10 users, ongoing since 2026-04-10)When
webSearchtimes out after 60s, the underlyinggenerateText()call to the AI Gateway continues running in the background indefinitely. NoAbortControllersignal is passed, so the HTTP request is never cancelled. Retries compound the problem since each one leaves behind another orphaned request leaking connections and memory.Root Cause
withTimeout()usesPromise.raceto implement the deadline, but the losing promise (generateText) is never told to stop. The same file (network.ts) already usesAbortControllercorrectly forfetchTextWithRedirects— the search tool just wasn't wired the same way.Fix
network.ts—withTimeoutonTimeoutcallback for post-timeout cleanup.onTimeoutruns in atry/catch— so the abort can never race the timeout error, and a throwing cleanup callback cannot break timeout semantics.search.ts—createWebSearchToolAbortControllerper search call.abortSignal: controller.signaltogenerateText()so the AI SDK cancels the underlying HTTP request on abort.onTimeout: () => controller.abort()so the abort fires immediately after the timeout rejection.Tests
AbortSignalis aborted on timeout.abortSignalassertion to the happy-path call-shape test.Supersedes #304 (closed due to shallow-clone force-push breaking branch history).
Closes JUNIOR-1H