Not signed in
Copyright © 2026 Unity Technologies
Fix native plugin crash when on non-main thread
Reproduction steps:
1. Open the attached “CrashRepro” project
Expected result: The project opens
Actual result: Unity Editor crashes
Reproducible with: 2020.2.7f1, 2020.3.43f1, 2021.1.0f1, 2021.1.28f1, 2021.2.0a20, 2021.2.19f1, 2021.3.16f1, 2022.1.0a3, 2022.1.24f1, 2022.2.1f1, 2023.1.0a23
Not reproducible with: 2020.2.6f1, 2021.1.0a1, 2021.1.0b12, 2021.2.0a1, 2021.2.0a19, 2022.1.0a1, 2022.1.0a2
Reproducible on: Windows 11 Pro 22H2
First lines of the stack trace:
0x00007FF667D1AD11 (Unity) RegisterPlugin 0x00007FF667D18577 (Unity) FindAndLoadUnityPlugin 0x00007FFF49BA44F3 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\native-library.c:1206] legacy_probe_for_module 0x00007FFF49BA4983 (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\native-library.c:1297] legacy_lookup_native_library 0x00007FFF49BA503A (mono-2.0-bdwgc) [C:\build\output\Unity-Technologies\mono\mono\metadata\native-library.c:1430] lookup_pinvoke_call_impl
Note: 2021.3.16f1, 2022.1.24f1, 2022.2.1f1 and 2023.1.0a23 crashes silently without any stack trace in Editor.log
UPDATE 2/21/2023:
This crash is caused by the ColorPicker scene. It can crash when opening the project if the ColorPicker is the default scene. A workaround is to move the calls to the Mat constructor on lines 32-33 in IN-23231_cv_app\Assets\course\App\Color Picker\ColorPicker.cs into the Start() function.
Native plugins are loaded as needed (i.e. will be loaded on the first invocation of a function in the native plugin). Usually this happens in Awake/Start/Update functions which will cause the native plugin to be loaded as part of the player loop that runs on the main thread. The ColorPicker is invoking the Mat constructor (in the opencv plugin) when declaring fields in the ColorPicker class, which causes the opencv plugin to be loaded when the scene is loaded on an asset loading worker thread. This crashes because native plugin loading requires access to the GfxDevice (see [Internal link]) so this must happen on the main thread or thread that has access to the GfxDevice.
We could output an error message that we are trying to load a native plugin on a different thread, but a better solution would be to not load native plugins on asset worker threads. We should follow up with the asset loading team to see if this is possible and makes sense. We probably still want to output a warning message and update the docs to explain that users should not call into native plugins outside the main thread.
Sign in to see your voted issues