I’m using some Unity services such as Cloud Save, Authentication, Analytics, Friends, Cloud Diagnostics, IAP and Ads Mediation.
I noticed that anytime the app runs in background, it will get the error with WebSocket.
How to deal with this issue?
WebSocketInvalidStateException: WebSocket is not in open state.
Unity.Services.Wire.Internal.WebSocket.Send (System.Byte[] data) (at <00000000000000000000000000000000>:0)
Unity.Services.Wire.Internal.Client.HandleServerPing () (at <00000000000000000000000000000000>:0)
Unity.Services.Wire.Internal.Client.OnWebsocketMessage (System.Byte[] payload) (at <00000000000000000000000000000000>:0)
This seems to be an issue on Wire’s side, we are looking at it internally, thank you for reporting it!
We’ll try to squeeze in a fix in an upcoming releases as soon as we can.
Note this might take a few weeks, as we have to first release Wire, then all the other services that depend on it.
In the meantime could you confirm if this stops your app from functioning, or everything continues to work as expected? I’m asking because Wire should be able to restore the connection after the first (pong message) failure and if it does not then we have to dig a bit deeper.
If everything seems to work fine, and you want to temporarily patch your game, you can always catch exceptions by doing something like outlined in this two api docs from microsoft:
Although my application continues to work correctly and delivers the expected results, I’ve noticed that it consistently throws an error in the Unity console. After some investigation, I determined that the error doesn’t directly interfere with the core functionality of the app. As a result, I’ve implemented a temporary workaround by suppressing the error message so it doesn’t disrupt development or clutter the console output.
Thanks again!
The good news is that Wire 1.4.0 has been released since, and should include a fix for this.
While there has not been a new cloud code release yet, you can add wire to your project manifest manually, and reference the latest package version, that should solve that faulty error message.
In case you are stuck on wire 1.3.0 for some reason could you try with the TaskScheduler.UnobservedTaskException too?
This is by far my most common Crash in Crashlytics. Is the above suggesting that it is not something to be too concerned about from an experience POV? I am yet to update to Unity 6 and other associated plugin updates as I don’t want to upset my app before launch, but it would be heartening to know it is not something wrong in my code and my users don’t suffer.
Fatal Exception: java.lang.Exception: WebSocketInvalidStateException : WebSocket is not in open state.
at Unity.Services.Wire.Internal.WebSocket.Send(Unity.Services.Wire.Internal.WebSocket)
at Unity.Services.Wire.Internal.Client.HandleServerPing(Unity.Services.Wire.Internal.Client)
at Unity.Services.Wire.Internal.Client.OnWebsocketMessage(Unity.Services.Wire.Internal.Client)
at System.Threading.Tasks.Task.Execute(System.Threading.Tasks.Task)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task)
at System.Threading.Tasks.Task.ExecuteEntry(System.Threading.Tasks.Task)
at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke(UnityEngine.UnitySynchronizationContext+WorkRequest)
at UnityEngine.UnitySynchronizationContext.Exec(UnityEngine.UnitySynchronizationContext)
at Unity.Services.Wire.Internal.Client.WebsocketMessageListener(Unity.Services.Wire.Internal.Client)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run(System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner)
at System.Threading.ThreadPoolWorkQueue.Dispatch(System.Threading.ThreadPoolWorkQueue)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_1(System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext)
at System.Threading.ThreadPoolWorkQueue.Dispatch(System.Threading.ThreadPoolWorkQueue)
Based on the Fatal Exception: java.lang.Exception: I’m guessing you are building for android.
To give you some context:
The exception originates from a disposed socket connection that the Wire package is attempting to use to send a ping message to the remote Wire service when the app returns from sleep. This was patched in Wire 1.4.0 (Which should be compatible with 2021.3+ you can add it to your project manifest by name).
Note that the fix does not apply for WebGL
Normally when Wire attempts to send a message and it fails, it will attempt to reestablish the connection. Unless the app was in sleep mode for minutes your game should be able to restore it’s connection, otherwise you might want to reset the game for the user, and ask them to reconnect.