Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EFCore 9.0 AccessViolationException #35331

Open
TonyValenti opened this issue Dec 13, 2024 · 3 comments
Open

EFCore 9.0 AccessViolationException #35331

TonyValenti opened this issue Dec 13, 2024 · 3 comments

Comments

@TonyValenti
Copy link

File a bug

Hi .NET and EFCore Team:
Since upgrading to .NET 9.0 and EFCore 9.0, we are sporadically getting AccessViolationExceptions which seem to stem from EFCore.

Include your code

I'm not sure about specifics of the code that is causing this as it seems to be all EFCore related

Include stack traces

Here is the error information from the Windows system logs:

Application: UniversalMigrator.Clio.exe
CoreCLR Version: 9.0.24.52809
.NET Version: 9.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack:
   at System.Runtime.ExceptionServices.InternalCalls.<RhpSfiInit>g____PInvoke|0_0(System.Runtime.StackFrameIterator*, Void*, Int32, Boolean*)
   at System.Runtime.ExceptionServices.InternalCalls.<RhpSfiInit>g____PInvoke|0_0(System.Runtime.StackFrameIterator*, Void*, Int32, Boolean*)
   at System.Runtime.EH.DispatchEx(System.Runtime.StackFrameIterator ByRef, ExInfo ByRef)
   at System.Runtime.EH.RhThrowHwEx(UInt32, ExInfo ByRef)
   at System.Linq.Enumerable+EnumerableSorter`2[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ComputeKeys(System.__Canon[], Int32)
   at System.Linq.Enumerable+OrderedIterator`2[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.UpdateReferenceMaps(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry, Microsoft.EntityFrameworkCore.EntityState, System.Nullable`1<Microsoft.EntityFrameworkCore.EntityState>)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(Microsoft.EntityFrameworkCore.EntityState, Microsoft.EntityFrameworkCore.EntityState, Boolean, Boolean)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>, System.Func`2<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>,Boolean>)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>, System.Func`2<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>,Boolean>)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>, System.Func`2<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>,Boolean>)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>, System.Func`2<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>,Boolean>)
   at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.TrackGraph[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Object, System.__Canon, System.Func`2<Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntryGraphNode`1<System.__Canon>,Boolean>)
   at UniversalMigrator.Agents.ConcurrentInjectorAgent9`4[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Storage_Attach[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](UniversalMigrator.Storage.MigrationDbContext, System.__Canon)
   at UniversalMigrator.Agents.ConcurrentInjectorAgent9`4+<ProcessItemAsync>d__15[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[UniversalMigrator.Agents.ConcurrentInjectorAgent9`4+<ProcessItemAsync>d__15[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], UniversalMigrator, Version=24.12.8.855, Culture=neutral, PublicKeyToken=null]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Threading.Tasks.UnwrapPromise`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetFromTask(System.Threading.Tasks.Task, Boolean)
   at System.Threading.Tasks.UnwrapPromise`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Invoke(System.Threading.Tasks.Task)
   at System.Threading.Tasks.Task.RunOrQueueCompletionAction(System.Threading.Tasks.ITaskCompletionAction, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at UniversalMigrator.Agents.ConcurrentInjectorAgent9`4+<WatchItemAsync>d__14[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[UniversalMigrator.Agents.ConcurrentInjectorAgent9`4+<WatchItemAsync>d__14[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], UniversalMigrator, Version=24.12.8.855, Culture=neutral, PublicKeyToken=null]].MoveNext(System.Threading.Thread)
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
   at System.Threading.Tasks.Task.RunContinuations(System.Object)
   at System.Threading.Tasks.SafeDelay+<DelayAsync>d__3.MoveNext()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Threading.Tasks.SafeDelay+<DelayAsync>d__3, System.Extensions, Version=24.12.8.855, Culture=neutral, PublicKeyToken=null]].MoveNext(System.Threading.Thread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()

Include provider and version information

EF Core version: 9.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET 9.0
Operating system: Win11
IDE: N/A

@ChrisJollyAU
Copy link
Contributor

@TonyValenti Need a bit more information - so the question is, what is your code doing when it calls EF Core?

Looking at the stack trace we have the following 2 lines just before any EF Core calls

   at UniversalMigrator.Agents.ConcurrentInjectorAgent9`4[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Storage_Attach[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](UniversalMigrator.Storage.MigrationDbContext, System.__Canon)
   at UniversalMigrator.Agents.ConcurrentInjectorAgent9`4+<ProcessItemAsync>d__15[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

This is your code so we need to know what you are trying to do there.

As a tip, your entry point to EF Cre in thsi case seems to be the call to ChangeTracker.TrackGraph so hopefully this helps you find the right place in your code to tell us what you aretrying to do there (and its surrounds)

@roji
Copy link
Member

roji commented Dec 14, 2024

To add to @ChrisJollyAU's answer, EF itself does not contain or invoke any native code (which is the only way an AccessViolationException would be triggered); the database provider (e.g. SqlClient in this case) may do native calls, but that's outside of EF's scope. With such memory corruption issues, an application bug completely unrelated to EF could cause memory corruption that would manifest as an access violation with an EF stack trace, even if EF itself has nothing to do with it - basically anything's possible once you have a native memory corruption.

I'd advise first trying to understand where native calls are being done in your applicaiton, and ideally trying to isolate some sort of repro - that's of course very hard as the errors are sporadic, though a test that loops many items over the problematic code can sometimes semi-reliably trigger the error etc.

@AndriySvyryd
Copy link
Member

If there's some issue that's causing this it would be in .NET runtime. Make sure to use the latest one and if you are still getting it try to create a minimal repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants