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

Exception 'System.Runtime.InteropServices.SEHException' #1865

Closed
sunthx opened this issue May 21, 2019 · 10 comments · Fixed by #2787
Closed

Exception 'System.Runtime.InteropServices.SEHException' #1865

sunthx opened this issue May 21, 2019 · 10 comments · Fixed by #2787
Assignees

Comments

@sunthx
Copy link

sunthx commented May 21, 2019

Goals

I'm using RealmDb with my wpf application . sometimes the database will throw an exception ,I don't know how to reproduce it.

Expected Results

succeed

Actual Results

UnhandledException : System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
   在 Realms.SynchronizationContextEventLoopSignal.EventLoop.<>c__DisplayClass3_0.<Post>b__0(Object _)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   在 System.Threading.ThreadPoolWorkQueue.Dispatch() 

Steps to Reproduce

I don't know how to reproduce this problem.

Version of Realm and Tooling

  • Realm.Database 3.4.0
  • Realm.DataBinding 1.2.0
  • Win10 1809
  • .Netframework 4.6.2
@Jared-Aus
Copy link

I get this error when trying to use Realm with xUnit. nUnit doesn't complain however.

@fealebenpae
Copy link
Member

Hey @Jared-Aus, can you upload a small project that reproduces the crash?

@Jared-Aus
Copy link

XUnitTestProject1.zip

@Jared-Aus
Copy link

Hey @Jared-Aus, can you upload a small project that reproduces the crash?

Done. Btw, I haven't noticed any issues in my Xamarin or nUnit projects, however it's possible that xUnit is simply less forgiving than the others.

@fealebenpae
Copy link
Member

I was able to reproduce this, thanks.
From what I can see the test passes fine, but during teardown the internal notification machinery crashes. I wasn't able to attach the exception in a debugger, unfortunately. Perhaps it's a quirk of the xUnit runner that we'll need to work around.

@Jared-Aus
Copy link

I was able to reproduce this, thanks.
From what I can see the test passes fine, but during teardown the internal notification machinery crashes. I wasn't able to attach the exception in a debugger, unfortunately. Perhaps it's a quirk of the xUnit runner that we'll need to work around.

No problems, and yes, that was my experience too.

@andrewdewaal
Copy link

andrewdewaal commented Mar 26, 2020

I have the same issue; running a simple test of querying against an empty Realm with an InMemoryConfiguration successfully queries, and then throws an SEHException:


 Message: 
    System.Runtime.InteropServices.SEHException : External component has thrown an exception.
  Stack Trace: 
    <>c__DisplayClass3_0.<Post>b__0(Object _)

Has anyone found a fix to put in the teardown to allow for writing xUnit tests with Realm?

@Silic0nS0ldier
Copy link

I've hit the same issue with xUnit. As others have observed the test cases run fine, but once done the issue manifests 100% of the time.

Message: 
  System.Runtime.InteropServices.SEHException : External component has thrown an exception.
Stack Trace: 
  SynchronizationContextScheduler.scheduler_invoke_function(IntPtr function_ptr)
  <>c__DisplayClass4_0.<Post>b__0(Object _)

From the trace I managed to pinpoint where the exception is raised.

scheduler_invoke_function(function_ptr);

Based on the .NET documentation for the SEHException class, something is going wrong in Realm Core or another native library.

Any SEH exception that is not automatically mapped to a specific exception is mapped to the SEHException class by default.

If able to catch that exception, we'll likely be closer to fixing this.

Version of Realm and Tooling

Realm 5.1.2 (via NuGet)
Win10 20H2
.NET Core 3.1 and .NET 5.0

@IngweLand
Copy link

I have just got the same issue. What actually worked for me is when you return async Task in your Fact/Theory instead of void, even if the code inside the test case does not use anything async related.

@nirinchev
Copy link
Member

My guess is that the synchronization context that xunit installs by default gets into an invalid state after the test completes, which means that if Realm tries to invoke anything from it, it will fail. This probably works with the async test because the synchronization context it installs for those behaves differently on teardown. We should try to repro and probably wrap the method calls in SynchronizationContextScheduler in try-catch to handle situations like these.

@nirinchev nirinchev assigned nirinchev and unassigned fealebenpae Jan 28, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants