Skip to content

Keep transaction cleanup independent of request cancellation#5574

Open
mikaelweave wants to merge 6 commits into
mainfrom
mikaelweave/fix-sql-transaction-cleanup-token
Open

Keep transaction cleanup independent of request cancellation#5574
mikaelweave wants to merge 6 commits into
mainfrom
mikaelweave/fix-sql-transaction-cleanup-token

Conversation

@mikaelweave
Copy link
Copy Markdown
Contributor

@mikaelweave mikaelweave commented May 15, 2026

Summary

  • We want to try to commit the current transaction during the exception flow in MergeInternalAsync and not let a cancellation exception bubble up an unhandled exception.
  • Discussed with @SergeyGaluzo
  • This is an edge case that can raise an error of trying to use an already closed transaction - see attached WI.

Changes

  • SqlServerFhirDataStore.cs: pass CancellationToken.None to MergeResourcesCommitTransactionAsync in the MergeInternalAsync single-transaction catch block.
  • Catch request cancellation during the retriable failure logging/delay block, fall through to failed transaction cleanup, then rethrow cancellation after cleanup.
  • This prevents an already-cancelled request token from skipping server-side transaction failure marking..

AB#188321

Testing

  • dotnet test src\Microsoft.Health.Fhir.SqlServer.UnitTests\Microsoft.Health.Fhir.SqlServer.UnitTests.csproj --no-restore --verbosity minimal
  • dotnet build src\Microsoft.Health.Fhir.SqlServer\Microsoft.Health.Fhir.SqlServer.csproj --no-restore --nologo --verbosity quiet
  • git diff --check

Move the SqlServerFhirDataStore failed-transaction cleanup token change out of PR #5553 so it can be reviewed independently from the BundleHandler cancellation response mapping.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.31%. Comparing base (b696902) to head (6bd20df).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5574      +/-   ##
==========================================
- Coverage   77.37%   77.31%   -0.06%     
==========================================
  Files         993      996       +3     
  Lines       36418    36539     +121     
  Branches     5518     5539      +21     
==========================================
+ Hits        28177    28251      +74     
- Misses       6880     6918      +38     
- Partials     1361     1370       +9     

see 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mikaelweave mikaelweave added this to the FY26\Q4\2Wk\2Wk24 milestone May 28, 2026
@mikaelweave mikaelweave added Bug Bug bug bug. Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs No-ADR ADR not needed No-PaaS-breaking-change labels May 28, 2026
@mikaelweave mikaelweave marked this pull request as ready for review May 28, 2026 21:36
@mikaelweave mikaelweave requested a review from a team as a code owner May 28, 2026 21:36
mikaelweave and others added 4 commits May 28, 2026 15:01
Remove test changes not required for the cancelled-retry-delay cleanup regression: revert ResourceWrapper search/compartment/claims to null, drop the expanded mock provider compartment setup, and remove a redundant Assert.NotNull already covered by Assert.IsAssignableFrom.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When the client cancels during the retriable merge-failure retry delay, let the
OperationCanceledException propagate without running MergeResourcesCommitTransactionAsync.
The TransactionWatchdog rolls abandoned transactions forward/back, so inline cleanup is
redundant here. This removes the ExceptionDispatchInfo capture/rethrow dance.

Simplify the cancellation test to use an NSubstitute mock instead of a hand-written
ISqlRetryService fake, restore the shared ModelInfoProvider compartment setup needed by
sibling tests, and add a focused regression test asserting non-cancellation cleanup still
runs with CancellationToken.None.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug. No-ADR ADR not needed No-PaaS-breaking-change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants