GetThreadContext failed GC error

So I have an irritating problem with the Unity engine lately. My Unity got a bit unstable and crashed on a small project. After that I couldn’t get it to run any project at all and got the above error.

After some research and tinkering with the built in code editor i managed to get Unity up and running using a different IDE than the MonoDevelop that’s shipped with Unity, but unfortunately the engine is broken again today with no apparent cause of failure.

I did look up the problem, and all I got is either “disable your antivirus” or “doubleclick a scene file”. I don’t run any anitivirus program on my dev PC and invoking unity by trying to open a scene file returns same error.

Additional info:

  • My dev desktop is running Windows 8.1 with no antivirus software

  • I tried reinstalling Unity with no effect

  • I can get into project manager just fine

  • 100% crash rate on opening or creating projects

I really hope there are any workarounds for this. The inability to use my main workstation is really stalling my college work. :frowning:

Some info from a non-unity dev who’s written GC code:

This is most likely the result of a race condition within the Unity GC. Basically, the way GC typically works is that the garbage collector maintains a list of threads in the system. When a collection begins, the GC will iterate through this list and suspend all threads, get info on what memory they’re referencing via GetThreadContext (on Windows), then it scans this memory to find what’s referenced, and then it resumes all the suspended threads.

GetThreadContext will basically only fail if you call it on a thread that isn’t suspended, so the most likely scenario is that there’s a case in Unity where a running thread can be added to this internal list after all threads in the list have been suspended but before the GC has done a sweep to call GetThreadContext on them. The GC then tries to get the context of a thread that should be suspended but isn’t and kaboom.

GC scans are typically triggered when you allocate memory. The memory manager sees that it doesn’t have enough to give you what you’ve asked for, so it collects, checks again, and if there still isn’t enough then it gets more from the OS.

So in this case, since we’re talking about a race condition, any change to when an allocation triggers a collection vs. when threads are starting will affect the likelihood of this error occurring. This is why people have reported this being related to Kaspersky. Virus detectors sometimes basically connect to apps with a debugger, which changes the timing of when things occur, so disabling a virus detector when this crash is happening may “fix” the problem.

Is there any way to run Unity in single core mode? If there aren’t multiple CPU cores executing threads simultaneously, there’s a chance that this problem will go away. Otherwise, assuming my analysis is correct, any real fix will have to come from a fix to the Unity GC. I’m guessing this is a home-grown GC and not a public one like the Boehm GC?

1 Like

Apologies for raising the dead on this thread but this one was by far the closest I’ve gotten to an actual answer on this issue outside of “virus scanners or bust” fixes.

I have the same issue but only occurs when using Parallels. Other co-workers that use a variety of Windows hardware configuration / setups don’t see the Error. Yet when I run it via Parallels on my Macbook Pro using Windows 10, I see it in a repeatable instances. Yet, exact same code running natively outside Parallels and under OSX Unity nothing occurs.