Repository navigation
Reset ConcurrencyDetector when a pooled context is returned - #38995
Conversation
Fixes dotnet#38983 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mq2nwPJvGQb6LEyRLVFWKP
There was a problem hiding this comment.
🟡 Changes recommended
A critical concurrency race remains in ConcurrencyDetector exception cleanup.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Updates pooled DbContext reuse by resetting ConcurrencyDetector state and making critical-section bookkeeping exception-safe.
Changes:
- Adds detector reset support.
- Resets the detector through
StateManager. - Adds pooling and nesting regression tests.
File summaries
| File | Summary |
|---|---|
test/EFCore.Tests/Infrastructure/Internal/ConcurrencyDetectorTest.cs |
Adds regression coverage. |
src/EFCore/Infrastructure/Internal/ConcurrencyDetector.cs |
Implements reset and safer enter/exit handling. |
src/EFCore/ChangeTracking/Internal/StateManager.cs |
Resets the detector during context reset. |
Review details
Suppressed comments (1)
src/EFCore/Infrastructure/Internal/ConcurrencyDetector.cs:80
- Resetting these counters makes a disposer from the previous lease indistinguishable from one created by the next lease. If an async query is still in flight when the context is returned (the PR explicitly allows the next lease to proceed), its
using var _ = EnterCriticalSection()will later callExitCriticalSection; after the new lease has entered, that old exit can decrement the new_currentContextRefCountand clear_inCriticalSectionwhile the new operation is active, disabling the guard. The reset/disposer protocol needs to invalidate old sections or otherwise prevent reuse until they exit.
public virtual void ResetState()
{
_currentContextRefCount = 0;
_inCriticalSection = 0;
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
@dotnet-policy-service agree |
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mq2nwPJvGQb6LEyRLVFWKP
|
On the suppressed note about |
There was a problem hiding this comment.
🟡 Changes recommended
Active disposers from a prior lease can corrupt the detector state after pooled reuse.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
However, since this is for an unsupported scenario, the fix won't be ported to earlier versions.
|
Hi @AndriySvyryd , why wouldn't this be ported on 10 and 11. |
|
@LuKuanTsen It's not a supported scenario, so it doesn't meet the servicing bar |
Fixes #38983 Co-authored-by: kuantsen.lu <[email protected]>
Fixes #38983
ConcurrencyDetectorwas never reset when a pooledDbContextwas returned to the pool, andEnterCriticalSection/ExitCriticalSectiontoggled_inCriticalSectionaround an allocatingAsyncLocal<int>write. If that write threw (in practiceOutOfMemoryException, see #22802), the flag stayed set, the context went back to the pool as usual, and every later lease of that instance failed with "A second operation was started on this context instance" for the life of the process.Two independent changes, no behaviour change on the happy path and no public API change:
ConcurrencyDetectorimplementsIResettableService, andStateManager.ResetState()resets it alongside_changeDetector. A context now always starts its next lease with a clear detector, whatever happened on the previous one. (The DI-registration variant,IConcurrencyDetector : IResettableService, is described in the issue; it is cleaner but changes public API, so this PR keeps the servicing-friendly shape. Happy to switch if preferred.)EnterCriticalSectionreleases the flag ifThreadAcquiredLocksCount.Value++throws before a disposer is handed out (only when this call took the flag, i.e. it is not a re-entrant call).ExitCriticalSectionclears the flag before the AsyncLocal decrement instead of after.One behaviour note for reviewers: the reset in (1) is unconditional. A context returned to the pool while an operation is still genuinely in flight used to throw on the next lease; now the next lease proceeds.
StateManager.ResetState()already clears tracked state under such an orphaned operation, so the old exception was late and attributed to the wrong request rather than a real guard.Tests (
ConcurrencyDetectorTest):Pooled_context_can_be_reused_after_critical_section_was_never_exited: red before, green after.ResetState_releases_critical_section_that_was_never_exited.Nested_critical_sections_are_released_when_outermost_exits: regression fence for theExitreorder.🤖 Generated with Claude Code
https://claude.ai/code/session_01Mq2nwPJvGQb6LEyRLVFWKP