Skip to content

Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures#37941

Draft
JoasE wants to merge 5 commits intodotnet:mainfrom
JoasE:fix/cosmos-session-token-pre-condition
Draft

Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures#37941
JoasE wants to merge 5 commits intodotnet:mainfrom
JoasE:fix/cosmos-session-token-pre-condition

Conversation

@JoasE
Copy link
Contributor

@JoasE JoasE commented Mar 17, 2026

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

@JoasE JoasE marked this pull request as ready for review March 17, 2026 11:40
@JoasE JoasE requested a review from a team as a code owner March 17, 2026 11:40
Copilot AI review requested due to automatic review settings March 17, 2026 11:40
@JoasE JoasE marked this pull request as draft March 17, 2026 11:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CosmosException headers when wrapping a 412 (PreconditionFailed) into DbUpdateConcurrencyException.
  • Preserve response headers when synthesizing a CosmosException for 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.

@JoasE JoasE marked this pull request as ready for review March 17, 2026 11:53
Copilot AI review requested due to automatic review settings March 17, 2026 11:53
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CosmosException headers on HttpStatusCode.PreconditionFailed (optimistic concurrency conflict).
  • Propagate transactional batch response headers into the created CosmosException so 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 AutoTransactionBehavior modes.

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.

@JoasE JoasE marked this pull request as draft March 18, 2026 09:30
@JoasE JoasE changed the title Cosmos: Track session tokens for pre condition failures Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures Mar 18, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TrackSessionToken to accept string? 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cosmos: Manual session token management, eventual consistency on reload after pre condition failure

3 participants