Fix LINQ Take() silently modifying cached QueryRequestOptions objects#5292
Fix LINQ Take() silently modifying cached QueryRequestOptions objects#5292
Conversation
Co-authored-by: kundadebdatta <87335885+kundadebdatta@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Please follow the required format: "[Internal] Category: (Adds|Fixes|Refactors|Removes) Description"
Internal should be used for PRs that have no customer impact. This flag is used to help generate the changelog to know which PRs should be included. Examples:
Diagnostics: Adds GetElapsedClientLatency to CosmosDiagnostics
PartitionKey: Fixes null reference when using default(PartitionKey)
[v4] Client Encryption: Refactors code to external project
[Internal] Query: Adds code generator for CosmosNumbers for easy additions in the future.
|
Hi @kundadebdatta — this Copilot-authored draft ($(System.Collections.Hashtable.copilotpr)) has been idle ~287 days with no human owner driving it. Would you (or someone on the team) be willing to pick this up, or should we close it? Flagged as part of an open-PR cleanup pass. |
Problem
The LINQ
Take()method was silently modifying the originalQueryRequestOptionsobject passed by users, causing performance issues when the same options object was cached and reused across multiple queries.Root Cause
In
CosmosQueryClientCore.ExecuteItemQueryAsync(), the code directly modified the originalQueryRequestOptions.MaxItemCountproperty:Reproduction Scenario
This resulted in:
Take(): Works fine withMaxItemCount = -1Take(1): Works but silently setsMaxItemCount = 1Take(): Very slow becauseMaxItemCount = 1forces one item per requestSolution
Modified
CosmosQueryClientCore.ExecuteItemQueryAsync()to create a shallow copy of theQueryRequestOptionsbefore modifyingMaxItemCount. This ensures the original cached object remains unchanged.Key Changes
QueryRequestOptionsinstance copying all properties from the originalMaxItemCountpropertyCode Changes
Testing
Added unit test
MaxItemCountNotModifiedInOriginalQueryRequestOptions_SimpleCopyTest()that verifies:QueryRequestOptionsproperties remain unchanged after copyingMaxItemCountvalueImpact
QueryRequestOptionsobjects are no longer modifiedFixes #5225.
💡 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.