EF Core bug causes concurrency errors under load due to service lifetime issue
A bug in Entity Framework Core causes an 'InvalidOperationException: A second operation was started on this context' error specifically under production load. The issue arises when a scoped DbContext is incorrectly reused across parallel work items, such as in a hosted background service. The root cause is a service-lifetime design flaw where fire-and-forget tasks capture and hold the DbContext after its intended scope has expired. This corrupts the context's internal state, leading to intermittent failures that are not seen during local, non-concurrent testing. The solution requires designing for lifetime correctness by resolving the DbContext inside each task's own scope.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in