Solana.Unity-SDK
Solana.Unity-SDK copied to clipboard
Unity editor hangs when losing blockchain connection.
Describe the bug Unity Editor is frozen when local blockchain connection is lost.
To Reproduce Steps to reproduce the behavior:
- Start local test validator.
- Enter Play Mode on Unity Editor.
- Connect to Local Net.
- Terminate the local test validator.
- [Editor Hang]
Expected behavior
- Unity error output on lost connection.
- Editor remains responsive.
Desktop:
- OS:
MacOS - Version:
Ventura 13.5.1
Additional context
Unity editor version: 2021.3.32f1
I narrowed the cause of the hang to the WebSocket RPC connection. If we disconnect from there, the main thread is not left hanging when connection to the RPC node gets interrupted.
The next thing to figure out: How to automatically invoke a disconnection when the RPC connection is suddenly unreachable?
Perhaps this is the answer: https://stackoverflow.com/questions/26971026/handling-connection-loss-with-websockets
When listening to socket messages you need to switch back to mainthread before accessing any unity functions. You can do that with await UniTask.SwitchToMainThread(); @GabrielePicco would be good to add this to the subscribe function and maybe that also makes i hang?
Thanks @kilogold, definitely helpful context. On unsubscribe, there is the logic to try-reconnect (which is needed as websockets sometimes lose connection), but there could be something wrong in the handling if the rpc is unreachable. I will investigate it. @Woody4618 you are right, this is not the intended behaviour. I opened an issue to fix it: https://github.com/magicblock-labs/NativeWebSocket/issues/2