fix: defer stream context cancel to iterator cleanup#688
Open
adek05 wants to merge 2 commits intogoogleapis:mainfrom
Open
fix: defer stream context cancel to iterator cleanup#688adek05 wants to merge 2 commits intogoogleapis:mainfrom
adek05 wants to merge 2 commits intogoogleapis:mainfrom
Conversation
sendStreamRequest defers cancel() on the timeout context, which fires when the function returns — before the iterator consumes any chunks. This causes the HTTP connection to be torn down after the first buffered chunk, silently dropping subsequent SSE events. The test sends 4 SSE chunks with 10ms delays between flushes and a 5s timeout. Without the fix only chunk 1 is received.
sendStreamRequest created a timeout context and deferred cancel(), which ran when the function returned — before the iterator had consumed any SSE chunks. This cancelled the request context and tore down the HTTP connection, causing multi-chunk streaming responses to be silently truncated after the first buffered event. Move the cancel function into responseStream so it is called when the iterator completes, alongside resp.Body.Close(). On error paths in sendStreamRequest the cancel is called immediately.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
@Sivasankaran25 hey, is there any process of getting this addressed or PRs merged or is this just a parking lot. |
Collaborator
Thanks you for your contributing. This PR has been filed internally, and reviews are in progress |
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
When
HTTPOptions.Timeoutis set,sendStreamRequestcreates a timeout context and immediately defers its cancellation. SincesendStreamRequestreturns before the caller iterates the stream, the deferredcancel()fires while chunks are still being received, causingresp.Body.Read()to fail with "context canceled" after the first buffered chunk.This means any streaming request with a timeout set loses all chunks except the first one.
Root Cause
BEFORE (broken):
sendStreamRequest returns → defer cancel() fires → context dead
iterator starts → Scan() gets chunk 1 (buffered) → Scan() fails on chunk 2
AFTER (fixed):
sendStreamRequest returns → cancel stored in responseStream
iterator starts → reads all chunks → defer cleanup calls cancel() + Close()
Test Plan:
Added TestSendStreamRequestTimeoutNotCancelledEarly: httptest server sends 4 SSE chunks with 10ms delays between flushes, 5s timeout. Asserts all 4 chunks are received.
Verified the test fails without the fix (only receives chunk 1)