Address review follow-ups on the async enclave attestation gate - #4621
Conversation
- Drop the now-unused Microsoft.Data.Common usings from the Azure and VSM providers, left behind when their redundant overrides were removed. - Use the TimeSpan overload of Task.Delay in both async retry loops. - Correct the ThreadRetryCache comment: it records thread IDs for the sync path, not the attestation url and nonce. - Document that the async gate deliberately does not share the sync path's adaptive lock timeout, so neither path can degrade the other. - Make the async gate timeout overridable so tests can drive the timeout fallthrough without waiting out the production timeout. - Add tests for the gate timeout fallthrough and for async re-attestation after a session is invalidated, plus a concurrency high-water mark on the fake so collapsing and fallthrough are asserted directly rather than by timing. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 148862f6-f66a-4a89-8436-ec4a008bbea3
There was a problem hiding this comment.
🟡 Changes recommended
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Addresses review follow-ups for the asynchronous enclave attestation gate.
Changes:
- Uses explicit
TimeSpanretry delays and removes unused imports. - Documents sync/async gate separation and corrects retry-cache documentation.
- Adds timeout, concurrency, and invalidation tests.
File summaries
| File | Description |
|---|---|
SqlColumnEncryptionEnclaveProviderAsyncShould.cs |
Adds gate and invalidation tests. |
VirtualSecureModeEnclaveProviderBase.cs |
Removes an unused import. |
VirtualSecureModeEnclaveProvider.cs |
Clarifies retry-delay units. |
EnclaveProviderBase.cs |
Documents and exposes the testable gate timeout. |
AzureAttestationBasedEnclaveProvider.cs |
Removes an import and clarifies retry-delay units. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| Task<SqlEnclaveSession> blocked = Task.Run(() => AttestAsync(provider, blockedParameters)); | ||
|
|
||
| // Reaching two attestations while the first is still parked is only possible if the second | ||
| // caller gave up on the gate. Without the fallthrough this wait times out. | ||
| await provider.WaitForAttestationCountAsync(2); | ||
| Assert.Equal(2, provider.MaxConcurrentAttestations); | ||
|
|
||
| hold.SetResult(true); | ||
| provider.HoldAttestation = null; | ||
|
|
||
| SqlEnclaveSession heldSession = await holder; | ||
| SqlEnclaveSession blockedSession = await blocked; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Review details
Suppressed comments (2)
Previously missed (1) — in code that hasn't changed since the last review.
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AlwaysEncrypted/SqlColumnEncryptionEnclaveProviderAsyncShould.cs:788
- Add an XML summary for this new test helper. Test helper methods in this repository are required to document their behavior and side effects; this one updates both the active-attestation count and its high-water mark.
This issue also appears on line 809 of the same file.
private void EnterAttestation()
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AlwaysEncrypted/SqlColumnEncryptionEnclaveProviderAsyncShould.cs:809
- Add the required XML summary for this new test helper as well, so its counter side effect is documented consistently with the other helpers in this file.
private void ExitAttestation() => Interlocked.Decrement(ref _concurrentAttestations);
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Balanced
priyankatiwari08
left a comment
There was a problem hiding this comment.
Concerns, all in test code. Nothing blocking.
-
Failed assert strands the static gate. In
CreateEnclaveSessionAsync_WhenGateWaitTimesOut_AttestsAnyway, ifWaitForAttestationCountAsync(2)or theMaxConcurrentAttestationsassert fails,hold.SetResult(true)never runs andholderparks forever holdings_asyncAttestationGate. Every later async attestation in the assembly then eats the 15s fallthrough, soConcurrentColdStartfails too — one failure cascades. Wrap intry { ... } finally { hold.TrySetResult(true); await Task.WhenAll(holder, blocked); }. -
Static gate, instance timeout hook.
s_asyncAttestationGateis static butAsyncAttestationGateTimeoutInMillisecondsis an instance member, so the effective timeout on a process-wide semaphore depends on whichever provider is waiting. Fine today because xUnit serialises within a class; add a[Collection]marker so a future parallelism change doesn't silently make these flaky. -
AttestationStartedis never reset or disposed and is only everSet(), so it can't be used to wait for a second attestation start. Doesn't affect these two tests, but the helper looks reusable. -
WaitForAttestationCountAsyncdoc says "Spins until" but it awaitsTask.Delay(10).
| // Reaching two attestations while the first is still parked is only possible if the second | ||
| // caller gave up on the gate. Without the fallthrough this wait times out. | ||
| await provider.WaitForAttestationCountAsync(2); | ||
| Assert.Equal(2, provider.MaxConcurrentAttestations); |
There was a problem hiding this comment.
This is the one thing I'd like changed before merge. Between here and hold.SetResult(true) there is no try/finally, and at this point the holder task is parked inside s_asyncAttestationGate, which is static.
If WaitForAttestationCountAsync(2) throws its 30s timeout assert, or this Assert.Equal fails, hold is never completed. The holder never returns from CreateEnclaveSessionCoreAsync, so CreateEnclaveSessionAsync's finally never runs and the semaphore is never released. Because the gate is static, that leak outlives this test: every later async attestation in the assembly then blocks for the full GateTimeoutInMilliseconds and falls through, so CreateEnclaveSessionAsync_ConcurrentColdStart_CompletesWithoutDeadlock's Assert.Equal(1, provider.MaxConcurrentAttestations) would start failing too. One real failure turns into a cascade of confusing unrelated failures, which is exactly the debugging experience this PR is otherwise trying to remove.
Wrapping the body from the Task.Run for holder down through the awaits in a try with finally { hold.TrySetResult(true); } fixes it. TrySetResult rather than SetResult also avoids an InvalidOperationException masking the original assertion failure if both paths run.
| // How long a caller waits for the async attestation gate before giving up and attesting on its | ||
| // own. Overridable so that tests can exercise the timeout fallthrough without waiting out the | ||
| // full production timeout; production providers use the default. | ||
| protected virtual int AsyncAttestationGateTimeoutInMilliseconds => LockTimeoutMaxInMilliseconds; |
There was a problem hiding this comment.
No objection to the seam — EnclaveProviderBase is internal, so this is not a public API addition, and the default keeps production behaviour identical.
One asymmetry worth a word in the comment: the gate this timeout applies to (s_asyncAttestationGate) is static, but the timeout is an instance member. So the wait duration for a process-wide semaphore is determined per-provider-instance. That is a non-issue in production because every provider inherits the same LockTimeoutMaxInMilliseconds, but it is exactly the kind of thing a future provider could override to a small value and thereby weaken collapsing for every other provider's callers as well. A one-line note here saying "instance-scoped override of a process-wide gate; production providers must not narrow this" would prevent that.
Stacked on #4541. Addresses the remaining open review comments there.
Changes
using Microsoft.Data.Common;fromAzureAttestationBasedEnclaveProvider.csandVirtualSecureModeEnclaveProviderBase.cs. They became unused when the redundant overrides were deleted.Task.DelayunitsTimeSpan.FromSeconds(...)instead ofx * 1000.ThreadRetryCachestores thread IDs for the sync path, not the attestation url and nonce. Comment corrected.LockTimeoutMaxInMillisecondsdirectly and never reads or writes the sync path's adaptivelockTimeoutInMilliseconds. The decoupling is symmetric: async callers cannot degrade that value for sync callers, and sync contention cannot collapse the async timeout to zero.Tests
CreateEnclaveSessionAsync_WhenGateWaitTimesOut_AttestsAnyway- a caller that cannot take the gate within the timeout attests on its own instead of failing or deadlocking, and does not release a gate it never held.GetEnclaveSessionAsync_AfterInvalidation_ReattestsAndReturnsNewSession- after invalidation the next async caller re-attests and gets a new session, which is then cached.To support the first test,
AsyncAttestationGateTimeoutInMillisecondsis aprotected virtualhook onEnclaveProviderBaseso a test provider can shorten the wait. Production providers use the default 15s.The fake provider also tracks a high-water mark of concurrent attestations. Collapsing and fallthrough are now asserted directly (
MaxConcurrentAttestationsof 1 vs 2) instead of inferred from timing, and the fallthrough test parks the gate holder on aTaskCompletionSourcethe test controls rather than sleeping. Verified both tests fail if the fallthrough is removed.Not included
@mdaigle's larger suggestion to converge the sync and async paths onto one primitive was marked "Not for this PR". It needs the sync path reshaped first (acquire/release within
CreateEnclaveSession, post-gate cache re-check, dropping the cross-call handoff and the timeout mutation) and a change toEnclaveSessionCache.CreateSessionto return an existing entry rather than overwrite. Worth a follow-up issue.Checklist