-
Notifications
You must be signed in to change notification settings - Fork 525
GatewayStoreClient: Fixes stream consumption bug in GatewayStoreClient.CreateDocumentClientExceptionAsync #5291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
…ExceptionAsync Co-authored-by: kirankumarkolli <[email protected]>
Copilot
AI
changed the title
[WIP] GatewayStoreClient.CreateDocumentClientExceptionAsync throws unhandled System.InvalidOperationException: The stream was already consumed. It cannot be read again.
Fix stream consumption bug in GatewayStoreClient.CreateDocumentClientExceptionAsync
Jul 15, 2025
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/GatewayStoreClientTests.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good!
Co-authored-by: kirankumarkolli <[email protected]>
kirankumarkolli
approved these changes
Jul 16, 2025
Member
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
ananth7592
approved these changes
Jul 16, 2025
kirankumarkolli
approved these changes
Jul 16, 2025
This was referenced Oct 6, 2025
Merged
This was referenced Oct 18, 2025
This was referenced Nov 9, 2025
This was referenced Nov 17, 2025
This was referenced Dec 1, 2025
This was referenced Dec 12, 2025
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
During write region failover, Gateway returns a 403 response with
application/jsoncontent type but invalid JSON content. This triggers a bug inGatewayStoreClient.CreateDocumentClientExceptionAsyncwhere the HTTP response stream is consumed twice:InvalidOperationException: The stream was already consumed. It cannot be read again.The original
DocumentClientExceptionwith proper diagnostics is lost, making debugging difficult.Root Cause
The issue was introduced when an
elseclause was removed and an emptycatchblock was added to the JSON deserialization logic, causing the same stream to be processed twice if deserialization fails.Solution
This PR implements a minimal fix that:
ReadAsStringAsync()before attempting JSON deserialization<e>to<Error>Changes Made
CreateDocumentClientExceptionAsyncmethod to buffer content once and reuse itTestStreamConsumptionBugFixWhenJsonDeserializationFailsto verify the fixCode Example
Before (buggy):
After (fixed):
This ensures that when Gateway returns invalid JSON during failover scenarios, clients get proper
DocumentClientExceptioninstances with diagnostics instead of unhandledInvalidOperationExceptionerrors.Fixes #5243.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.