You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SqlQueryMetadataCache: splits the lookup into an in-memory probe (TryGetCachedQueryMetadata) and CompleteCachedQueryMetadataAsync, which loads keys one by one with async key store calls.
Stale key: the entry is evicted and the lookup is a miss.
Canceled: the exception propagates and good metadata stays cached.
Other errors: the entry is evicted and the error is rethrown.
SqlCommand.PrepareForTransparentEncryption: on an async cache hit, keys load on a worker (Task.Run). A stale key falls back to describe parameter encryption on that same worker, never on the caller thread. The sync path is unchanged.
Review fixes, for the window while cached keys load:
_enclaveAttestationInProgress stays set until execution starts and is cleared on success, failure and cancellation. Until then, changing Connection or Transaction, or starting a second execution, throws.
Execution is rejected if the connection was replaced, closed and reopened, or switched with ChangeDatabase. This is checked before the stale-key fallback and again before execution.
ValidateConnectionForExecute runs before the stale-key fallback describe.
Out of scope: the gap between the write completing and the read stage publishing PendingAsyncOperation already exists on main for every async path (open thread).
Deferred to layer 5: async enclave package and key loading, and retry changes.
New UnitTests/Microsoft/Data/SqlClient/SqlQueryMetadataCacheTests.cs: async hit, stale and cancel cases, plus sync hit and stale cases.
SqlCommandTests: worker dispatch on an async cache hit, and stale-key fallback.
New SimulatedServerTests/SqlCommandQueryMetadataCacheAsyncTests.cs: 7 guard tests using a gated key store provider. They cover setter and second-execution rejection, the guard being released on cancel, reopen before execution and before the fallback, a reader opened before the fallback, and ChangeDatabase.
dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj -f net10.0 -c Debug|Release --filter "FullyQualifiedName~SqlQueryMetadataCacheTests|FullyQualifiedName~SymmetricKeyCacheTests|FullyQualifiedName~SqlSecurityUtilityTests|FullyQualifiedName~SqlCommandTests|FullyQualifiedName~SqlCommandQueryMetadataCacheAsyncTests|FullyQualifiedName~SqlCommandEnclaveAsyncTests": 160/160 passed in each configuration. On macOS this ran with a local, uncommitted TDS listener patch, because the simulated server hangs on shutdown there.
UnitTests: Linux net10 2668 passed, Windows net10 2692 passed, net462 2588 passed, 0 failed. Simulated-server tests ran and none of the stack's tests were skipped.
AE manual tests: 246 passed, 0 failed. Only the enclave tests were skipped, because CI has no enclave server.
Guidelines
Please review the contribution guidelines before submitting a pull request:
Split the metadata cache lookup into an in-memory probe and an async key
load. Async execution loads cached column encryption keys on a worker,
falls back to describe parameter encryption when keys are stale, and
keeps good metadata cached when the load is canceled.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical concurrency and connection-lifetime gaps during cached-key loading can let execution use changed connection state or fallback RPCs corrupt an active reader’s results.
This fourth layer of async Always Encrypted support extends asynchronous key loading to query metadata cache hits, while preserving synchronous provider dispatch.
Changes:
Splits cache lookup into an in-memory probe and asynchronous key-loading completion.
Dispatches async cache completion and stale-key discovery fallback to a worker.
Adds coverage for cache hits, stale keys, cancellation, and worker dispatch.
❌ Patch coverage is 82.11009% with 39 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.81%. Comparing base (86b2cdc) to head (8dbd424).
Keep _enclaveAttestationInProgress set while cached keys load and until
execution reserves the connection, so connection changes and overlapping
executions are rejected. Reject a close/reopen or connection replacement
before the stale-key fallback and before execution, and revalidate the
connection before the fallback describe.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical races allow command state changes before pending-operation publication and database changes during cached-key loading, potentially executing with altered state or writing to the wrong database.
The reason will be displayed to describe this comment to others. Learn more.
This window exists on main (2bda3c9) already, so I'm leaving the thread open.
In SqlCommand.Reader.cs, BeginExecuteReaderInternal runs BeginExecuteReaderInternalReadStage only after writeTask completes (lines 290-334). SetActiveConnectionAndResult, which sets PendingAsyncOperation, runs only in the read stage (line 400).
The setters and validation (SqlCommand.cs 605, 758, 2902, 2924) check only _enclaveAttestationInProgress or PendingAsyncOperation. On main nothing sets either during an async write, plain or after the uncached describe continuation (Reader.cs ~1837). Only enclave attestation sets the flag.
This PR doesn't widen the window. The cached path now holds the guard until RunExecuteReaderTds returns. After that it has the same write-to-read-stage tail as the uncached and plain async-write paths on main. Closing that tail for every path is a separate change.
Capture the connection database before cached-key completion and reject
execution if ChangeDatabase ran while keys loaded, because the query
metadata cache key includes the database.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
If connection.BeginTransaction() succeeds while a cached key load is pending, the command can resume inside that transaction even though command.Transaction is null, instead of throwing TransactionRequired. Revalidate transaction binding before stale-cache discovery and final execution, using the transaction checks from ValidateCommand without invoking its in-progress guard. Add a gated-provider regression test that begins a transaction before releasing the key load.
Retry deferred enclave-package failures on metadata-cache hits
On an async metadata-cache hit, a missing or expired enclave session can now make GenerateEnclavePackage fail in this continuation, bypassing the synchronous retry catch in RunExecuteReader. CreateLocalCompletionTask forwards that fault before reaching its retry handler, so a previously recoverable cached execution fails. Preserve metadata/session invalidation and the single retry for these deferred package failures, even if the remaining retry refactoring stays in layer 5.
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Every async cache hit now pays a Task.Run hop, even when KeysToLoad is empty (plaintext-only parameters, which the code notes is common) and likely when the symmetric key cache is warm, so the load completes synchronously. Consider calling the cache's CompleteCachedQueryMetadataAsync inline first: if it already completed with a hit, return returnTask = null as before, and offload only when it's pending or stale. That keeps the steady-state path free of the extra hop/allocations and keeps GenerateEnclavePackage inside RunExecuteReader's retry try for the common case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hotfix 8.0.0PRs that must be backported to release/8.0
4 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Layer 4 of 5 split from #4585. Stacked on #5080.
SqlQueryMetadataCache: splits the lookup into an in-memory probe (TryGetCachedQueryMetadata) andCompleteCachedQueryMetadataAsync, which loads keys one by one with async key store calls.SqlCommand.PrepareForTransparentEncryption: on an async cache hit, keys load on a worker (Task.Run). A stale key falls back to describe parameter encryption on that same worker, never on the caller thread. The sync path is unchanged.Review fixes, for the window while cached keys load:
_enclaveAttestationInProgressstays set until execution starts and is cleared on success, failure and cancellation. Until then, changingConnectionorTransaction, or starting a second execution, throws.ChangeDatabase. This is checked before the stale-key fallback and again before execution.ValidateConnectionForExecuteruns before the stale-key fallback describe.Out of scope: the gap between the write completing and the read stage publishing
PendingAsyncOperationalready exists on main for every async path (open thread).Deferred to layer 5: async enclave package and key loading, and retry changes.
Issues
Part of #4585.
Testing
UnitTests/Microsoft/Data/SqlClient/SqlQueryMetadataCacheTests.cs: async hit, stale and cancel cases, plus sync hit and stale cases.SqlCommandTests: worker dispatch on an async cache hit, and stale-key fallback.SimulatedServerTests/SqlCommandQueryMetadataCacheAsyncTests.cs: 7 guard tests using a gated key store provider. They cover setter and second-execution rejection, the guard being released on cancel, reopen before execution and before the fallback, a reader opened before the fallback, andChangeDatabase.dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj -f net10.0 -c Debug|Release --filter "FullyQualifiedName~SqlQueryMetadataCacheTests|FullyQualifiedName~SymmetricKeyCacheTests|FullyQualifiedName~SqlSecurityUtilityTests|FullyQualifiedName~SqlCommandTests|FullyQualifiedName~SqlCommandQueryMetadataCacheAsyncTests|FullyQualifiedName~SqlCommandEnclaveAsyncTests": 160/160 passed in each configuration. On macOS this ran with a local, uncommitted TDS listener patch, because the simulated server hangs on shutdown there.dotnet build src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj -f net462 -c Debug|Release: succeeded, 0 warnings, 0 errors.Guidelines
Please review the contribution guidelines before submitting a pull request: