efcore: ObjectDisposedException on retrieval of a pooled DbContext
EF Core version: 6.0.0-rc1 Database provider: any Target framework: .NET 6.0 Operating system: any IDE: any
https://github.com/dotnet/efcore/blob/release/6.0/src/EFCore/DbContext.cs#L902 - looks like _disposed
field should be set to false
here instead (and in the *Async version of the same method). The existing code causes any DbContext
that was returned back to the pool to throw ObjectDisposedException
on attempt to rent it again.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (9 by maintainers)
Commits related to this issue
- fix: https://github.com/dotnet/efcore/issues/26202 — committed to servicetitan/Stl.Fusion by alexyakunin 3 years ago
- fix: a better workaround for https://github.com/dotnet/efcore/issues/26202 — committed to servicetitan/Stl.Fusion by alexyakunin 3 years ago
- Fix context pooling concurrency issue Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 (cherry picked from commit 8e09d658b9748f9d1bfdde8bce1c516042dc3166) — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue (#26226) Fixes #26202 — committed to dotnet/efcore by roji 3 years ago
- Fix context pooling concurrency issue Fixes #26202 (cherry picked from commit 8e09d658b9748f9d1bfdde8bce1c516042dc3166) — committed to dotnet/efcore by roji 3 years ago
@roji Race condition:
Context is disposed:
Calls
_lease.ContextDisposed();
Context is now back in the pool. Back to Dispose method,
DisposeSync
is called:Context is marked as disposed and lease is deactivated. But by this time the context may already have been pulled from the pool again, so we deactivate a context that is in use.
@roji Can you prepare a PR cherry-picking this to 5.0.x?
FYI I don’t think this bug exists in 3.1 - it doesn’t seem like the DbContext is mutated in any way after being returned to the pool (code).
@roji , @ajcvickers , huge thanks - I also wanted to post a working example, but just got back to this & was pleasantly surprised!