You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into a deadlock when I'm deleting separate entities in parallel. From my understanding, this should work without any issues.
This might be related to #15180 or #14371 but they have already been fixed, and the changes should be in the version I'm using (6.0.6)
While I'm struggling to understand exactly what's causing the issue (my knowledge of SQL locks is pretty bad 😅), it appears to be related to the cascading deletes that are configured automatically. Disabling the cascading deletes seems to help, however, I can't always disable the cascading deletes since I have owned entities where I can't configure the delete behavior.
Please let me know if you need any more information.
Unhandled exception. System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' cal
l.
---> Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 60) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__188_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
ClientConnectionId:9d47ea57-6524-4452-9f44-4077c91a4d34
Error Number:1205,State:53,Class:13
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at DeadlockInvestigation.Program.<>c.<<Main>b__0_0>d.MoveNext() in C:\Users\yashanf\Code\DeadlockInvestigation\DeadlockInvestigation\Program.cs:line 82
--- End of stack trace from previous location ---
at DeadlockInvestigation.Program.<>c.<<Main>b__0_0>d.MoveNext() in C:\Users\yashanf\Code\DeadlockInvestigation\DeadlockInvestigation\Program.cs:line 83
--- End of stack trace from previous location ---
at System.Threading.Tasks.Parallel.<>c__50`1.<<ForEachAsync>b__50_0>d.MoveNext()
--- End of stack trace from previous location ---
at DeadlockInvestigation.Program.Main() in C:\Users\yashanf\Code\DeadlockInvestigation\DeadlockInvestigation\Program.cs:line 72
at DeadlockInvestigation.Program.<Main>()
Notes from triage: This is something that could potentially be helped by a query hint, although whether EF can do this automatically requires some investigation. Implementation of #6717 would allow easily adding query hints manually.
Adding a non clustered index on the child entities foreign key seems to have resolved the issue most of the time. Still observed a few occurrences in the logs but I think I might need to revisit other entity configuration. Those few scenarios involved a number of reads/writes within the same transaction seemingly and we are using RowVersions, so I'll need to tet to unpack it into more demo'able example...
Hi all,
I'm running into a deadlock when I'm deleting separate entities in parallel. From my understanding, this should work without any issues.
This might be related to #15180 or #14371 but they have already been fixed, and the changes should be in the version I'm using (6.0.6)
While I'm struggling to understand exactly what's causing the issue (my knowledge of SQL locks is pretty bad 😅), it appears to be related to the cascading deletes that are configured automatically. Disabling the cascading deletes seems to help, however, I can't always disable the cascading deletes since I have owned entities where I can't configure the delete behavior.
Please let me know if you need any more information.
Code
Stack trace
Deadlock report
Provider and version information
Database provider:
Microsoft.EntityFrameworkCore.SqlServer
- 6.0.6Target framework: .NET 6.0
Operating system: Windows 10 Pro x64 (19044)
SQL Server: Developer (64-bit) 15.0.2080.9
IDE: JetBrains Rider 2021.3.2
The text was updated successfully, but these errors were encountered: