Fix race condition in bearer-rejection retry that caused "link chain completed without emitting a value"#11754
Merged
Merged
Conversation
…k chain completed without emitting a value" When the server sends X-Bearer-Token-Rejected, BatchHttpLink calls observer.next(result) and then observer.complete() synchronously in the same Promise .then(). The next handler detected the rejection and kicked off an async token refresh without forwarding the value — but the synchronous complete: () => observer.complete() fired before the retry could emit, triggering Apollo Client 4's validateDidEmitValue guard. Fix: call activeSubscription.unsubscribe() synchronously inside the next handler the moment bearer rejection is detected. This marks the rxjs Subscriber as isStopped before BatchHttpLink's .complete() runs, so that call becomes a no-op. The retry then proceeds normally and emits a value through a fresh subscription. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is |
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.
Purpose
Apollo was throwing "The link chain completed without emitting a value" when loading pages in background tabs (or whenever the server sends
X-Bearer-Token-Rejected: trueon a response).The root cause was a race condition in
buildRefreshOnRejectedBearerLink.BatchHttpLinkcallsobserver.next(result)and then immediately callsobserver.complete()synchronously in the same Promise.then()callback. When thenexthandler detected a bearer rejection, it kicked off an async token refresh and returned — without ever callingobserver.next()on the outer observable. But the synchronouscomplete: () => observer.complete()handler fired before the retry had a chance to emit anything. Apollo Client 4'svalidateDidEmitValueguard sees this and throws.The intended defense (calling
activeSubscription.unsubscribe()at the top ofrunOnce()) only ran afterensureFreshAccessToken()resolved asynchronously — too late.Changes
💻 Engineer-facing
nexthandler, callactiveSubscription.unsubscribe()synchronously the moment rejection is detected. This marks the rxjsSubscriberasisStoppedbeforeBatchHttpLink's.complete()runs, making that call a no-op. The retry proceeds normally and emits a value through a fresh subscription.Background tabs were the most common trigger because bearer rejection is more likely when a token is near expiry — tabs that have been sitting open for a while naturally hit this more often.
Release plan and notes
🚢
🤖 Generated with Claude Code