jacksondunstan/UnityNativeScripting

Crash upon re-playing with Python plugin

sekkit opened this issue · 2 comments

hotreload works fine, but if stop playing, and re-play in Unity Editor, it crashes.

CODE:
void InitPy()
{
    System::String msg= System::String("py_start");
    UnityEngine::Debug::Log(msg);
    py::initialize_interpreter(true);
    //py::scoped_interpreter guard{};
    try{
        py::object mainScope = py::module::import("__main__").attr("__dict__");
        py::exec(\
                 "from clr import UnityEngine\n"
                 "UnityEngine.Debug.Log('hello')\n"
                 "import fast_calc\n"
                 "with open('pyinit.log', 'w') as f:\n"
                 "    f.write(str(fast_calc.add(1, 2)))\n", mainScope); 
    }
    catch(const py::error_already_set& exc)
    {
        System::String msg= System::String(exc.what());
        UnityEngine::Debug::Log(msg);
        PyErr_Print();
    }
    catch(const std::exception &exc)
    {
        System::String msg= System::String(exc.what());
        UnityEngine::Debug::Log(msg);
        //\npy::finalize_interpreter();
    }
    
    py::finalize_interpreter();
    System::String msg2= System::String("py_exit");
    UnityEngine::Debug::Log(msg2);
}


Process:               Unity [41049]
Path:                  /Applications/Unity/Unity.app/Contents/MacOS/Unity
Identifier:            com.unity3d.UnityEditor5.x
Version:               Unity version 2018.4.5f1 (2018.4.5f1)
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           Unity [41049]
User ID:               501

Date/Time:             2019-08-03 05:50:25.942 +0800
OS Version:            Mac OS X 10.14.6 (18G84)
Report Version:        12
Anonymous UUID:        6DB50D53-DA47-9FD4-6E53-08D77FBA2524


Time Awake Since Boot: 61000 seconds

System Integrity Protection: disabled

Crashed Thread:        0  CrBrowserMain  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGABRT)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000169b118c0
Exception Note:        EXC_CORPSE_NOTIFY

VM Regions Near 0x169b118c0:
    VM_ALLOCATE            0000000169884000-0000000169894000 [   64K] rwx/rwx SM=PRV  
--> 
    __TEXT                 0000000169ba0000-0000000169d24000 [ 1552K] r-x/rwx SM=COW  /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/Python

Application Specific Information:
abort() called

Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	0x00007fff6fd102c6 __pthread_kill + 10
1   libsystem_pthread.dylib       	0x00007fff6fdcbbf1 pthread_kill + 284
2   libsystem_c.dylib             	0x00007fff6fc7a6a6 abort + 127
3   com.unity3d.UnityEditor5.x    	0x0000000101053a21 HandleSignal(int, __siginfo*, void*) + 81
4   libmono.0.dylib               	0x000000016fac0bce mono_chain_signal + 93
5   libmono.0.dylib               	0x000000016fa07fd2 mono_sigsegv_signal_handler + 210
6   libsystem_platform.dylib      	0x00007fff6fdc0b5d _sigtramp + 29
7   com.unity3d.UnityEditor5.x    	0x0000000100589820 mpi_montmul + 1424
8   org.python.python             	0x0000000169bff9d2 PyUnicode_FromString + 14
9   org.python.python             	0x0000000169c7a9e2 _PySys_BeginInit + 1481
10  org.python.python             	0x0000000169c6c056 _Py_InitializeCore_impl + 775
11  org.python.python             	0x0000000169c6c505 _Py_InitializeCore + 184
12  org.python.python             	0x0000000169c6d222 _Py_InitializeFromConfig + 50
13  org.python.python             	0x0000000169c6d34c Py_InitializeEx + 74
14  NativeScript                  	0x0000000169783e36 pybind11::initialize_interpreter(bool) + 70 (embed.h:109)
15  NativeScript                  	0x0000000169783b2c InitPy + 60 (Game.cpp:69)
16  NativeScript                  	0x000000016978462a PluginMain(void*, int, bool) + 314 (Game.cpp:130)
17  NativeScript                  	0x00000001697d6c25 Init + 2117 (Bindings.cpp:6234)
18  ???                           	0x0000000169671ce7 0 + 6063332583
19  ???                           	0x0000000169670f99 0 + 6063329177
20  ???                           	0x000000016966d1ad 0 + 6063313325
21  ???                           	0x000000016966b34b 0 + 6063305547
22  ???                           	0x000000013e88907a 0 + 5344104570
23  libmono.0.dylib               	0x000000016fa09c5a 0x16fa00000 + 40026
24  libmono.0.dylib               	0x000000016fb3685e mono_runtime_invoke + 117
25  com.unity3d.UnityEditor5.x    	0x000000010108416a ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) + 106
26  com.unity3d.UnityEditor5.x    	0x000000010108401b ScriptingInvocation::InvokeChecked(ScriptingExceptionPtr*) + 59
27  com.unity3d.UnityEditor5.x    	0x00000001010373d0 MonoBehaviour::CallMethodInactive(ScriptingMethodPtr) + 592
28  com.unity3d.UnityEditor5.x    	0x000000010103c078 MonoBehaviour::CallAwake() + 120
29  com.unity3d.UnityEditor5.x    	0x000000010103c660 MonoBehaviour::AddToManager() + 384
30  com.unity3d.UnityEditor5.x    	0x00000001010ed29c AwakeFromLoadQueue::InvokePersistentManagerAwake(AwakeFromLoadQueue::Item*, unsigned int, AwakeFromLoadMode) + 796
31  com.unity3d.UnityEditor5.x    	0x00000001010ece36 AwakeFromLoadQueue::PersistentManagerAwakeFromLoad(int, AwakeFromLoadMode) + 342
32  com.unity3d.UnityEditor5.x    	0x0000000100d6be29 LoadSceneOperation::CompleteAwakeSequence() + 649
33  com.unity3d.UnityEditor5.x    	0x0000000100d6a522 LoadSceneOperation::IntegrateMainThread() + 514
34  com.unity3d.UnityEditor5.x    	0x0000000100d6dc32 PreloadManager::UpdatePreloadingSingleStep(PreloadManager::UpdatePreloadingFlags, int) + 530
35  com.unity3d.UnityEditor5.x    	0x0000000100d6e841 PreloadManager::WaitForAllAsyncOperationsToComplete() + 145
36  com.unity3d.UnityEditor5.x    	0x000000010137ed90 EditorSceneManager::RestoreSceneBackups(std::__1::vector<EditorSceneBackup, stl_allocator<EditorSceneBackup, (MemLabelIdentifier)111, 16> >&, EditorSceneManager::PlayModeChange, bool) + 1776
37  com.unity3d.UnityEditor5.x    	0x00000001019e10be PlayerLoopController::EnterPlayMode(bool) + 590
38  com.unity3d.UnityEditor5.x    	0x00000001019d940d PlayerLoopController::SetIsPlaying(bool) + 269
39  com.unity3d.UnityEditor5.x    	0x00000001019d81bd Application::TickTimer() + 6909
40  com.unity3d.UnityEditor5.x    	0x0000000100701dce -[EditorApplication TickTimer] + 142
41  com.apple.Foundation          	0x00007fff45f30c3b __NSFireTimer + 80
42  com.apple.CoreFoundation      	0x00007fff43ca3060 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
43  com.apple.CoreFoundation      	0x00007fff43ca2c0c __CFRunLoopDoTimer + 851
44  com.apple.CoreFoundation      	0x00007fff43ca2752 __CFRunLoopDoTimers + 330
45  com.apple.CoreFoundation      	0x00007fff43c83962 __CFRunLoopRun + 2130
46  com.apple.CoreFoundation      	0x00007fff43c82ebe CFRunLoopRunSpecific + 455
47  com.apple.HIToolbox           	0x00007fff42ee21ab RunCurrentEventLoopInMode + 292
48  com.apple.HIToolbox           	0x00007fff42ee1ded ReceiveNextEventCommon + 355
49  com.apple.HIToolbox           	0x00007fff42ee1c76 _BlockUntilNextEventMatchingListInModeWithFilter + 64
50  com.apple.AppKit              	0x00007fff4127a79d _DPSNextEvent + 1135
51  com.apple.AppKit              	0x00007fff4127948b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
52  com.apple.AppKit              	0x00007fff412735a8 -[NSApplication run] + 699
53  com.apple.AppKit              	0x00007fff41262ae8 NSApplicationMain + 777
54  com.unity3d.UnityEditor5.x    	0x000000010072a0fc EditorMain(int, char const**) + 844
55  com.unity3d.UnityEditor5.x    	0x000000010072a579 main + 9
56  libdyld.dylib                 	0x00007fff6fbd53d5 start + 1

Thread 1:
0   libsystem_pthread.dylib       	0x00007fff6fdc83f0 start_wqthread + 0

Thread 2:: Profiler.Dispatcher
0   libsystem_kernel.dylib        	0x00007fff6fd0a266 semaphore_wait_trap + 10
1   libdispatch.dylib             	0x00007fff6fb88bd9 _dispatch_sema4_wait + 16
2   libdispatch.dylib             	0x00007fff6fb8939f _dispatch_semaphore_wait_slow + 98
3   com.unity3d.UnityEditor5.x    	0x0000000102a5a7b8 PlatformSemaphore::WaitForSignal() + 24
4   com.unity3d.UnityEditor5.x    	0x0000000100f3febc profiling::Dispatcher::ThreadFunc(void*) + 76
5   com.unity3d.UnityEditor5.x    	0x0000000100e71a62 Thread::RunThreadWrapper(void*) + 1282
6   libsystem_pthread.dylib       	0x00007fff6fdc92eb _pthread_body + 126
7   libsystem_pthread.dylib       	0x00007fff6fdcc249 _pthread_start + 66
8   libsystem_pthread.dylib       	0x00007fff6fdc840d thread_start + 13

From the stack trace, it looks like the crash occurred inside Python's pybind11::initialize_interpreter. Since that's outside of the realm of this project, you might want to dig into that code or ask the Python team for help.

After removing cpp source folder, this issue is gone...