fix(ai-gateway): distinguish client disconnects from upstream disconnects - #4795
Merged
Merged
Conversation
…ects A client going away aborts the upstream fetch through the forwarded request signal, so those cancellations were reported as "The upstream provider disconnected before sending a response" right after logging the client disconnect. Return a dedicated 499 client_disconnect response for that case, split the upstream message into timeout and connection-close variants, and note in the client-disconnect log that later aborts follow from it.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryReviewed the client-disconnect vs. upstream-disconnect classification changes across the gateway route, upstream fetch handling, error-type enum, and tests, and found no high-confidence bugs, security issues, or logic errors in the changed lines. Files Reviewed (4 files)
Reviewed by claude-sonnet-5 · Input: 16 · Output: 5K · Cached: 404.7K Review guidance: REVIEW.md from base branch |
marius-kilocode
approved these changes
Jul 27, 2026
johnnyeric
approved these changes
Jul 27, 2026
This was referenced Jul 27, 2026
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
When a client disconnects mid-request, the request signal is forwarded to the upstream fetch, so the fetch aborts too. The gateway logged
AI gateway client disconnectedand then immediately returnedThe upstream provider disconnected before sending a response.— implying an upstream fault that never happened.The same message was also used for gateway-side timeouts (no response headers within 15 minutes), which is a different failure.
Changes
upstream-request.ts: when the fetch fails while the caller's signal is aborted (and the failure is an abort, or classification itself failed), return a dedicatedclient_disconnectresponse with HTTP 499 and the message "The client disconnected before the upstream provider responded, so the request was cancelled. The upstream provider did not fail." 499 keeps these cancellations out of upstream 5xx error rates.error_type: upstream_disconnectand HTTP 503.route.ts: the client-disconnect log now says it is aborting in-flight upstream work, with a comment recording that any abort logged afterwards for that request is a consequence of the disconnect.client_disconnecttoProxyErrorType.Sentry/console reporting behaviour is unchanged: client-caused aborts are still not captured as upstream fetch failures.
Verification
pnpm --filter web test -- src/tests/openrouterApi.timeout.test.ts(extended with client-disconnect, gateway-timeout, and connection-close response assertions)pnpm --filter web test -- src/app/api/openrouterpnpm --filter web typecheck,pnpm --filter web lint,pnpm format