Solana.Unity-SDK icon indicating copy to clipboard operation
Solana.Unity-SDK copied to clipboard

Unity editor hangs when losing blockchain connection.

Open kilogold opened this issue 1 year ago • 4 comments

Describe the bug Unity Editor is frozen when local blockchain connection is lost.

To Reproduce Steps to reproduce the behavior:

  1. Start local test validator.
  2. Enter Play Mode on Unity Editor.
  3. Connect to Local Net.
  4. Terminate the local test validator.
  5. [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

kilogold avatar Apr 09 '24 22:04 kilogold

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?

kilogold avatar Apr 26 '24 19:04 kilogold

Perhaps this is the answer: https://stackoverflow.com/questions/26971026/handling-connection-loss-with-websockets

kilogold avatar Apr 26 '24 19:04 kilogold

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?

Woody4618 avatar Apr 26 '24 22:04 Woody4618

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

GabrielePicco avatar May 07 '24 10:05 GabrielePicco