Skip to content

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

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

Cosmos: Track session tokens for Pre-Condition, Conflict and document NotFound failures#37941
JoasE wants to merge 9 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.

@JoasE JoasE marked this pull request as ready for review March 19, 2026 08:18
Copilot AI review requested due to automatic review settings March 19, 2026 08:18
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

This PR improves Cosmos session-token handling to ensure session tokens are captured from certain failure responses (PreconditionFailed/Conflict/document NotFound), so follow-up reads/reloads in manual/semi-automatic session-token modes avoid stale reads.

Changes:

  • Update CosmosClientWrapper to track session tokens from selected failure responses (and track on read-item NotFound) in addition to success paths.
  • Relax ISessionTokenStorage.TrackSessionToken to accept nullable tokens and make SessionTokenStorage.TrackSessionToken ignore null/whitespace tokens.
  • Add/adjust unit and functional tests to validate the new session-token tracking behavior, including concurrency and NotFound scenarios.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs Tracks session tokens on Conflict/PreconditionFailed/selected NotFound failures and on read-item NotFound/success paths.
src/EFCore.Cosmos/Storage/Internal/ISessionTokenStorage.cs Updates the interface contract to accept nullable session tokens.
src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs Ignores null/whitespace tokens during tracking; preserves FullyAutomatic behavior.
test/EFCore.Cosmos.Tests/Storage/Internal/SessionTokenStorageTest.cs Updates expectations/tests for null/whitespace token tracking behavior.
test/EFCore.Cosmos.FunctionalTests/TestUtilities/NullSessionTokenStorage.cs Aligns test utility implementation with new nullable session-token signature.
test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs Adds functional coverage for session-token updates after concurrency conflicts and NotFound scenarios.

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 19, 2026 08:45
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

This PR improves EF Core Cosmos session consistency when using manual session token management by ensuring session tokens from certain failure responses (precondition failed, conflict, and document NotFound) are tracked, so subsequent reload/read operations can observe the latest server state.

Changes:

  • Update ISessionTokenStorage.TrackSessionToken to accept nullable tokens and ignore null/whitespace.
  • Track session tokens from relevant Cosmos write failures and document-NotFound read responses in CosmosClientWrapper.
  • Adjust/add unit and functional tests to validate the new tracking behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/EFCore.Cosmos.Tests/Storage/Internal/SessionTokenStorageTest.cs Updates expectations around null/whitespace tokens being ignored rather than throwing.
test/EFCore.Cosmos.FunctionalTests/TestUtilities/NullSessionTokenStorage.cs Updates test storage implementation signature for nullable session tokens.
test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs Adds functional coverage for session token updates on precondition/conflict/NotFound scenarios.
src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs Implements nullable token handling by ignoring null/whitespace tokens.
src/EFCore.Cosmos/Storage/Internal/ISessionTokenStorage.cs Updates interface to accept nullable session tokens.
src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs Tracks session tokens from specific failure responses and from document-NotFound reads.

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 11:03
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

This PR improves EF Core Cosmos session consistency when using manual (or semi-automatic) session token management by ensuring the session token is tracked even when certain write/read operations fail (e.g., optimistic concurrency conflicts and “document not found”), so subsequent reloads/reads can observe the latest state.

Changes:

  • Update Cosmos request/response processing to track session tokens on specific failure responses (PreconditionFailed, Conflict, and relevant NotFound cases).
  • Relax ISessionTokenStorage.TrackSessionToken to accept nullable tokens and ignore null/whitespace tokens.
  • Add functional and unit tests covering session token tracking behavior for these failure scenarios and null/whitespace tokens.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs Tracks session tokens on selected failure responses and centralizes response processing for writes/batches/reads.
src/EFCore.Cosmos/Storage/Internal/ISessionTokenStorage.cs Makes TrackSessionToken accept string? to support cases where headers may be absent.
src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs Ignores null/whitespace tokens when tracking (and continues to no-op in FullyAutomatic mode).
test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs Adds functional coverage for session token updates on precondition/conflict/notfound scenarios.
test/EFCore.Cosmos.FunctionalTests/TestUtilities/NullSessionTokenStorage.cs Updates test utility implementation to match nullable TrackSessionToken signature.
test/EFCore.Cosmos.Tests/Storage/Internal/SessionTokenStorageTest.cs Updates unit tests to validate null/whitespace tokens are ignored instead of throwing.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 11:11
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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 11:20
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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

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