Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures#37941
Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures#37941JoasE wants to merge 5 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Updates Cosmos provider session-token handling so that when an optimistic concurrency conflict occurs (HTTP 412), the session token from the failure response is captured—allowing subsequent reload/retry operations to read the latest document version under session consistency.
Changes:
- Track the Cosmos session token from
CosmosExceptionheaders when wrapping a 412 (PreconditionFailed) intoDbUpdateConcurrencyException. - Preserve response headers when synthesizing a
CosmosExceptionfor failed transactional batches, so session tokens are available to higher layers. - Add a functional test asserting the session token is updated after an optimistic concurrency conflict (for both transactional and non-transactional save paths).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs | Adds a new test validating session-token updates after a concurrency exception. |
| src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs | Tracks session tokens from 412 responses before throwing DbUpdateConcurrencyException. |
| src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs | Copies transactional batch response headers onto the constructed CosmosException to preserve session token info. |
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Improves Cosmos session token tracking when an optimistic concurrency conflict occurs, ensuring the latest session token is recorded even when the failure comes from a transactional batch.
Changes:
- Track and persist the session token from
CosmosExceptionheaders onHttpStatusCode.PreconditionFailed(optimistic concurrency conflict). - Propagate transactional batch response headers into the created
CosmosExceptionso session tokens are available to the higher-level exception wrapper. - Add a functional test verifying the session token is updated after a concurrency exception across different
AutoTransactionBehaviormodes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs | Adds coverage to assert session token updates after a concurrency conflict and minor cleanup. |
| src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs | On concurrency conflicts, tracks the session token from the Cosmos response to support correct reload/retry behavior. |
| src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs | Copies transactional batch response headers onto the thrown CosmosException so session token data isn’t lost on failure paths. |
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Updates Cosmos session token handling to be more resilient by allowing null/whitespace tokens and by tracking session tokens from certain failure responses (and some read paths) to reduce the chance of subsequent stale reads.
Changes:
- Allow
TrackSessionTokento acceptstring?and treat null/whitespace as “no-op”. - Track session tokens on selected failure responses (e.g., conflict/precondition failed, some not-found cases) and refactor response processing in
CosmosClientWrapper. - Add/adjust unit + functional tests to cover the new session-token tracking behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.Cosmos.Tests/Storage/Internal/SessionTokenStorageTest.cs | Updates unit tests to assert null/whitespace tokens are ignored (no longer throw). |
| test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs | Adds functional coverage for session token updates after certain exceptions and not-found reads; removes unused usings/whitespace. |
| src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs | Makes TrackSessionToken nullable and ignores null/whitespace tokens. |
| src/EFCore.Cosmos/Storage/Internal/ISessionTokenStorage.cs | Updates interface contract to string? for session token tracking. |
| src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs | Tracks session tokens from specific failure/read responses; refactors write/batch/read response handling. |
You can also share your feedback on Copilot code review. Take the survey.
Tracks the session token returned from a pre-condition, Conflict and NotFound failure, so subsequent reloads are ensured to read the latest document on re-load
Closes: #37942