Strange exception in webGl build when tab is inactive

Hi there.
I have very simple scene with one gameObject with one script attached:

public class AuthTest : MonoBehaviour
{
    private async void Awake() {
        await UnityServices.InitializeAsync();
        AuthenticationService.Instance.SignedIn += onLogin;
        AuthenticationService.Instance.ClearSessionToken();
        await AuthenticationService.Instance.SignInAnonymouslyAsync();

        print("init done");
    }
    private void onLogin() {
        print("user id: " + AuthenticationService.Instance.PlayerId);
        print("accessToken: " + AuthenticationService.Instance.AccessToken);
        print("authed anonimously");
    }
}

When I do build & run, it authorizes with no errors, but if the browser tab is inactive for 2-3 minutes, it throws an exception:

Uncaught TypeError: Cannot read properties of undefined (reading 'readyState')
    at _WebSocketSend (bin.framework.js:10:75724)
    at bin.wasm:0xa4d359
    at bin.wasm:0xa42c00
    at bin.wasm:0x1e977d1
    at invoke_vii (bin.framework.js:10:401814)
    at bin.wasm:0xa4240b
    at bin.wasm:0xa44ad0
    at bin.wasm:0x13c20b5
    at bin.wasm:0x1e977d1
    at invoke_vii (bin.framework.js:10:401814)
    at bin.wasm:0x13c1815
    at bin.wasm:0x13c205c
    at bin.wasm:0x2f4130
    at bin.wasm:0x1e97803
    at invoke_viii (bin.framework.js:10:402109)
    at bin.wasm:0x13aa8cc
    at bin.wasm:0x13afd66
    at bin.wasm:0x1e97841
    at invoke_viiiii (bin.framework.js:10:404045)
    at bin.wasm:0x13c1eb2
    at bin.wasm:0x13c1b9d
    at bin.wasm:0x13c8c81
    at bin.wasm:0x1e97803
    at invoke_viii (bin.framework.js:10:402109)
    at bin.wasm:0x11463b6
    at bin.wasm:0x11462ca
    at bin.wasm:0x11465d0
    at bin.wasm:0x164f270
    at bin.wasm:0x16605b1
    at bin.wasm:0x1e977df
    at invoke_iiii (bin.framework.js:10:402400)
    at bin.wasm:0x165f652
    at bin.wasm:0x166175e
    at bin.wasm:0x1e4e5b9
    at bin.wasm:0x1df66ac
    at bin.wasm:0x1dc6d75
    at bin.wasm:0x16ef946
    at bin.wasm:0x16ef9b9
    at bin.wasm:0x1e7f07d
    at bin.wasm:0x1e9781b
    at browserIterationFunc (bin.framework.js:10:223047)
    at callUserCallback (bin.framework.js:10:176616)
    at Object.runIter (bin.framework.js:10:177856)
    at Browser_mainLoop_runner (bin.framework.js:10:176152)

The issue isn’t reproducible in Unity Editor.
After the error happens, app is completely unressponsive.
I guess I should fill a bug report.
Brief search didn’t help
Is this a known issue?

1 Like

Same thing happens to me. Using Unity 2022.3.24f.1. Error only happens in webgl, but it is not consistent for me and cannot reproduce consistently.

Same thing, happens on 2022.3.1 and Unity 6.

Finally fixed it after so many days. Thanks to @ [ZanthousDevelopment]

"I don’t know if this is bad to do but I took the .framework.js.gz, extracted the build.framework.js, edited the line (instance.ws===null)return-3 to (instance.ws==null)return-3 (from === to == comparison), repacked it and the error doesn’t crash the application anymore because this new comparison returns before the line that tries to check instance.ws.readyState when instance.ws is undefined.

This comparison appears twice, so the one in _WebSocketSend is the one that needs to be modified"

Check out his post here A simple CallEndpointAsync on CloudCodeService is not working in WebGL

1 Like

Experiencing the same issue with Unity 6