geekrelief / gdnim

godot-nim based bootstrapping framework supporting hot reloading
Other
55 stars 4 forks source link

Something in `Watcher` is causing instability in ORC. Crash occurs when generating thousands of node instances with hot reloading. #29

Closed geekrelief closed 3 years ago

geekrelief commented 3 years ago

Getting this random crash after hot reloading a bunch of times. Looks like nim ORC is trying either call a bad function in trace or gets bad data.

nim version: 3b963a8150e3c541859931a1ef9ecb338e46907f

ERROR: NativeScriptInstance detected crash on method: register_instance
   at: NativeScriptInstance::notification (modules\gdnative\nativescript\nativescript.cpp:735)
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[0] trace__Pgw8McKUNuhxTelmFLa61gsystem (C:\nim\lib\system\orc.nim:81)
[1] scanBlack__t5JPtD1AT5y7bGV1vv4KJA_3 (C:\nim\lib\system\orc.nim:164)
[2] scan__t5JPtD1AT5y7bGV1vv4KJA_2 (C:\nim\lib\system\orc.nim:240)
[3] collectCyclesBacon__cKo5xgNSZEVlhzdcplUDNA (C:\nim\lib\system\orc.nim:319)
[4] collectCycles__amVlU9ajqZ06ujoesRBHcDg_2 (C:\nim\lib\system\orc.nim:372)
[5] registerCycle__W1emNvKsDKJ8tfxEiEnOvg (C:\nim\lib\system\orc.nim:402)
[6] rememberCycle__LoYD9cYK9aJvrcDizBN64qaQ (C:\nim\lib\system\orc.nim:454)
[7] nimDecRefIsLastCyclicDyn (C:\nim\lib\system\orc.nim:465)
[8] eqdestroy___hwjDpahwYF1ImTW9axXRumw (C:\godot\gdnim\deps\godot\nim\godotmacros.nim:708)
[9] createFunc__dTdpNAKWMlJpoMqkqbdcTg (C:\godot\gdnim\deps\godot\nim\godotmacros.nim:709)
[10] main
[11] main
[12] main
[13] main
[14] call__Fcqhs1hhgvwTXPioAUEPjAgodotinternal (C:\godot\gdnim\deps\godot\godotinternal.nim:25)
[15] newOwnObj__9aMXOwroTmREicc52gFImkQ (C:\godot\gdnim\deps\godot\nim\godotnim.nim:410)
[16] gdnew__saqRYK8rD2rR1fcQIiqmdQ (C:\godot\gdnim\deps\godot\nim\godotnim.nim:437)
[17] register_instance__GU3pQzEYT1yfdTsRwpZS9bw (C:\godot\gdnim\gdnim\watcher.nim:319)
geekrelief commented 3 years ago

After doing some stress tests with generating lots of bullets, I can make various crashes occur in ORC/ARC. I tried instantiating 10K bullets on start and could only instance a little over 4K. I tried shortening the interval for bullet generation and managed to generate 3,148 before crashing ORC.

Then I tried disabling hot reloading, and the crashes disappeared. I'm guessing something is wrong in Watcher since in one of my tests a crash occurred while in the enter_tree method of bullet.

Another workaround with hot reloading, is to space the instantiation over time. Crashing is much more likely to occur when you need to create a lot of instances in a very short period of time.

geekrelief commented 3 years ago

When Watcher connects to a node, it seems ORC is prone to crash on reload when lots of nodes have been instanced. Switching to node_removed on SceneTree seems to be more stable (no crashing after instancing lots of nodes so far).

I tried switching back to tree_exiting on Node and used the binds argument on connect to pass in the instance id to avoid creating a closure to capture the instance id when unregistering. I tried disconnecting inside unregister_instance but still crashed.