fix(relay): handle WS errors during optimistic response streaming#24
Open
iceteaSA wants to merge 1 commit into
Open
fix(relay): handle WS errors during optimistic response streaming#24iceteaSA wants to merge 1 commit into
iceteaSA wants to merge 1 commit into
Conversation
When an optimistic response is already streaming (responseStartedAt is set), the pending promise was already resolved. Calling failPending() would reject a settled promise and leave the ReadableStream hanging with no more data and no close signal. Now injects an SSE error event into the stream and closes the socket cleanly, matching the existing upstream-4xx handling in response_start.
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
Fixes a bug where a relay-level WebSocket error arriving during an optimistic response that has already started streaming leaves the
ReadableStreamhanging indefinitely.The problem
When
optimisticResponseis true andresponse_starthas already been received (resolving the pending promise with a streamingResponse), a subsequent relayerrormessage callsfailPending(). But the promise is already resolved — the rejection goes nowhere, and theReadableStreamnever gets closed or errored. The client hangs waiting for data that will never arrive.The fix
Before falling through to
failPending(), check if the optimistic response is already streaming (responseStartedAt != null). If so:errorevent into the stream (same pattern as the existing upstream-4xx handling inresponse_start)finishPending()to close the stream cleanlyThis matches the existing handling for upstream HTTP 4xx errors at line 792, extending it to relay-level errors that arrive mid-stream.
Testing
All 214 tests pass. This is a race condition that occurs when the relay server encounters an error after already forwarding the upstream's
response_start— e.g., the worker hitting a CPU time limit mid-stream.