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 9 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.
There was a problem hiding this comment.
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
CosmosClientWrapperto track session tokens from selected failure responses (and track on read-item NotFound) in addition to success paths. - Relax
ISessionTokenStorage.TrackSessionTokento accept nullable tokens and makeSessionTokenStorage.TrackSessionTokenignore 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>
There was a problem hiding this comment.
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.TrackSessionTokento 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>
There was a problem hiding this comment.
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.TrackSessionTokento 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>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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