Skip to content

[Async AE 1] Add async key cache - #5077

Merged
cheenamalhotra merged 10 commits into
mainfrom
dev/cheena/async-ae-key-cache
Nov 18, 2026
Merged

cheenamalhotra merged 10 commits into
mainfrom
dev/cheena/async-ae-key-cache

Conversation

@cheenamalhotra

@cheenamalhotra cheenamalhotra commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Description

Layer 1/5 of the split of #4585: async symmetric-key cache foundation only. Adds one internal GetKey(..., bool isAsync, CancellationToken) entry point returning ValueTask (cache hits don't allocate), with cancellable async waits and one per-key load gate shared by sync and async callers. Same-key misses share one decryption; different keys load in parallel. With TTL zero (cache disabled), loads skip the gate.

Extracted from source head 5f1b860410426f8c1af3eb55bfac0e7354bd2ac7 onto main 2bda3c9edb53ce59589cdd401f10ae4415f331a5, preserving newer main changes. Execution-path integration follows in later layers; no public API or dependency changes.

Changes added while addressing review feedback:

  • Race fix: a waiting caller rechecks the cache after acquiring the gate, so a key published meanwhile is reused instead of decrypted again.
  • Per reviewer request, replaced the global s_cacheLock and _publicationLock with the per-key gate for both sync and async. Behavior change: providers may now be called concurrently for different keys, and sync zero-TTL loads are no longer serialized.
  • Removed the sync GetKey and GetKeyAsync wrappers: callers use the shared GetKey directly (sync passes isAsync: false, CancellationToken.None and calls .GetAwaiter().GetResult()). LoadKey follows the same shape. Provider TTL is set inside LoadKey's try, keeping the KeyDecryptionFailed wrapping from main.

Release note: custom SqlColumnEncryptionKeyStoreProvider implementations must be thread-safe, since DecryptColumnEncryptionKey may now run concurrently. Built-in providers (certificate store, CNG, CSP) keep no shared mutable state, and the AKV provider already synchronizes its caches.

Issues

Relates to #3672. Split from #4585; neither is closed by this layer.

Testing

All source GetKeyAsync_* coverage (now GetKey_*) moved to SymmetricKeyCacheTests (sync/async identity, cancellation, same-key waits, failed-owner retry, zero-TTL concurrency), plus regressions for publication while waiting, sync waiting on a same-key async load, and different-key sync loads not blocking. Provider fixtures live under UnitTests/Utilities.

Local (macOS, head 939291a):

dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net10.0 --configuration Debug --no-restore --filter 'FullyQualifiedName~SymmetricKeyCacheTests' --blame-hang-timeout 2m --verbosity quiet
# Passed: 18, failed: 0, skipped: 0.

dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net10.0 --configuration Release --no-restore --filter 'FullyQualifiedName~SymmetricKeyCacheTests' --blame-hang-timeout 2m --verbosity quiet
# Passed: 18, failed: 0, skipped: 0.

dotnet build src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj -f net462 -c Debug
# Passed: 0 errors, 0 warnings.

CI:

  • This PR on 7690d3c: sqlclient-pr build 193268 passed. Since then: 83f1a17 bounds the cross-key test wait; d2ef8c7..939291a share sync/async key loading through one ValueTask GetKey entry point. 63fbeb2 rewords a comment. CI pending on 63fbeb2.

  • Before the per-key gate change: sqlclient-pr build 192526 on 16919af passed all 39 checks; full stack [Async AE 5] Load enclave keys asynchronously and route package retries #5085 build 192590 on aa9fadbf. UnitTests: Linux net10 2668 passed / 0 failed; Windows net10 2692 / 0; net462 2588 / 0; simulated-server tests ran and no stack tests were skipped. AE manual tests: 246 passed / 0 failed; only enclave tests skipped (no enclave server in CI).

  • Tests added or updated

  • Public API changes documented (none)

  • Verified against customer repro (not applicable to this foundation layer)

  • Ensure no breaking changes introduced

Guidelines

Please review the contribution guidelines before submitting a pull request:

Extract the cache foundation from #4585 with per-key async loading, cancellation, and shared sync/async publication. Relocate cache regressions into focused cache tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 18:59
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 16, 2026
@cheenamalhotra cheenamalhotra added this to the 8.1.0-preview1 milestone Nov 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human verification is needed because the current PR identity, feedback and CI evidence, and a trusted revision of supplemental review policies were not established.

No open issues 🎉

What changed in this PR

Adds the asynchronous Always Encrypted symmetric-key cache foundation for #3672, as the first of five layers split from #4585. Command execution integration is deferred; this layer changes no public APIs or dependencies.

Changes:

  • Adds per-key load gates, cancellable waits, and concurrent uncached loads when the cache lifetime is zero.
  • Shares sync/async cache publication while serializing provider invocation without holding the gate during outstanding async I/O.
  • Adds 16 unit tests covering cache reuse, cancellation, retries, provider serialization, and publication races.
File Description
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Utilities/​SymmetricKeyCacheTestProviders.cs Adds observable synchronous and asynchronous test providers.
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Utilities/​PendingDecryptionKeyStoreProvider.cs Adds a controllable provider for overlapping decryption tests.
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​TestIsolationCollections.cs Isolates tests that change the process-wide cache lifetime.
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Microsoft/​Data/​SqlClient/​AlwaysEncrypted/​SymmetricKeyCacheTests.cs Adds async cache behavior and concurrency regression coverage.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​AlwaysEncrypted/​SymmetricKeyCache.cs Implements async loading, per-key coordination, and shared cache publication.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 19:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Required policy checks could not be completed because trusted supplemental review policies or references were unavailable, and head CI is still pending.

No open issues 🎉

Review effort: Balanced

Avoid repeating decryption when a synchronous caller publishes the key while an async caller waits for the provider gate. Cover the race with a deterministic regression test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 19:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Required policy checks could not be completed without trusted review references, while head CI remains pending and Windows/.NET Framework runtime coverage is unconfirmed.

No open issues 🎉

Review effort: Balanced

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.72131% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.20%. Comparing base (17bef7b) to head (63fbeb2).
⚠️ Report is 17 commits behind head on main.

Files with missing lines Patch % Lines
...ata/SqlClient/AlwaysEncrypted/SymmetricKeyCache.cs 96.63% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5077      +/-   ##
==========================================
+ Coverage   68.99%   70.20%   +1.21%     
==========================================
  Files         314      315       +1     
  Lines       45857    69952   +24095     
==========================================
+ Hits        31638    49111   +17473     
- Misses      14219    20841    +6622     
Flag Coverage Δ
CI-SqlClient ?
sqlclient-pr 70.20% <96.72%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cheenamalhotra
cheenamalhotra marked this pull request as ready for review November 17, 2026 06:28
@cheenamalhotra
cheenamalhotra requested a review from a team as a code owner November 17, 2026 06:28
@cheenamalhotra cheenamalhotra changed the title Add async Always Encrypted key cache (1/5) [Async AE 1] Add async key cache Nov 17, 2026
@cheenamalhotra cheenamalhotra added the Hotfix 8.0.0 PRs that must be backported to release/8.0 label Nov 17, 2026
@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board Nov 17, 2026
@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Nov 18, 2026
Replace the global cache lock and publication lock with one per-key gate
shared by GetKey and GetKeyAsync. Same-key misses share one decryption;
different keys load in parallel.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
mdaigle
mdaigle previously approved these changes Nov 18, 2026
- GetKeyAsync returns ValueTask so cache hits do not allocate.
- GetKey and GetKeyAsync share one core with bool isAsync.
- Set provider TTL inside LoadKey's try to keep KeyDecryptionFailed wrapping.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human compatibility review is needed because replacing global serialization with per-key gates changes concurrency for existing custom providers, including synchronous fallbacks and zero-TTL loads, while the new tests use concurrency-safe synthetic providers.

No open issues 🎉

Review effort: Balanced

benrr101
benrr101 previously approved these changes Nov 18, 2026
Remove the sync GetKey and GetKeyAsync wrappers. Callers use the shared
ValueTask GetKey with isAsync and a CancellationToken.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 18:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Replacing global provider serialization with per-key gates changes the concurrency contract for existing custom key-store providers, requiring human compatibility review of their thread safety.

No open issues 🎉

Review effort: Balanced

stacked auto-merge was automatically disabled November 18, 2026 18:38

Replaced by a newer auto-merge request

@cheenamalhotra
cheenamalhotra enabled stacked auto-merge (squash) November 18, 2026 18:38
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 18:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Required policy checks could not be completed without trusted references, and a maintainer must confirm acceptance of concurrent cross-key calls for custom providers and the release/migration communication.

No open issues 🎉

Review effort: Balanced

stacked auto-merge was automatically disabled November 18, 2026 19:38

Auto-merge was disabled for another pull request in the stack

@cheenamalhotra
cheenamalhotra merged commit cf4040a into main Nov 18, 2026
43 checks passed
@cheenamalhotra
cheenamalhotra deleted the dev/cheena/async-ae-key-cache branch November 18, 2026 20:15
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Nov 18, 2026
cheenamalhotra added a commit that referenced this pull request Nov 19, 2026
Main now contains layers 1-3 (#5077, #5079, #5080). Conflicts are resolved
to the reviewed final form of the stack (#5085 b8f9d12), which includes
layers 4 and 5. Old *AsyncShould tests are removed because they target APIs
the stack replaced; their coverage lives in the stack's tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 82aca8ca-2078-4a47-860c-6f6ee78cbb02
@mdaigle mdaigle mentioned this pull request Nov 19, 2026
3 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants