WebGL Context Lost

Hello everyone,

one of our clients is regularly encountering a issue, where our WebGLBuild crashes with the following console message:
WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost
Researching the error message, showed that the browser may decide to close a webgl context, if it thinks the gpu is under pressure and it can happen basically any time.

Our solution looked like:
Since only the Interface crashes, not the app itself, we basically stored the whole state in a variable and then restart the unity instance, by calling the loader again.

When manually crashing the webgl context by creating 16 other contexts it works fine. However, when the webgl context is lost normally, we get the following error message when restarting the unity instance:

20986a4044eaa8551db6be07233cc1d9.js:4 Creating WebGL 2.0 context.
20986a4044eaa8551db6be07233cc1d9.js:4 Could not create canvas: Failed to create a WebGL2 context.,:(,{"alpha":true,"depth":true,"stencil":true,"antialias":false,"premultipliedAlpha":false,"preserveDrawingBuffer":false,"preferLowPowerToHighPerformance":false,"failIfMajorPerformanceCaveat":false,"majorVersion":2,"minorVersion":0,"explicitSwapControl":0,"proxyContextToMainThread":0,"renderViaOffscreenBackBuffer":0,"powerPreference":"high-performance"}
20986a4044eaa8551db6be07233cc1d9.js:4 Unable to create WebGL context.
20986a4044eaa8551db6be07233cc1d9.js:4 exception thrown: TypeError: Cannot read properties of undefined (reading 'getParameter'),TypeError: Cannot read properties of undefined (reading 'getParameter')
    at _glGetString (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:293859)
    at wasm://wasm/06ea8e66:wasm-function[6874]:0x28d47a
    at wasm://wasm/06ea8e66:wasm-function[6853]:0x28ae43
    at wasm://wasm/06ea8e66:wasm-function[6817]:0x288c6f
    at wasm://wasm/06ea8e66:wasm-function[10958]:0x458a54
    at wasm://wasm/06ea8e66:wasm-function[10918]:0x450ef8
    at wasm://wasm/06ea8e66:wasm-function[10911]:0x44fce2
    at wasm://wasm/06ea8e66:wasm-function[10887]:0x44dbcb
    at wasm://wasm/06ea8e66:wasm-function[10750]:0x43b181
    at wasm://wasm/06ea8e66:wasm-function[10749]:0x43ad55
    at Object._main (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:495337)
    at Object.callMain (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:542024)
    at doRun (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:542700)
    at run (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:542868)
    at runCaller (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:541643)
    at Object.removeRunDependency (blob:URL/97db2fc4-0736-4197-9934-0b244079d6ff:8:21768)
    at processDataJob (URL/WebGLBuild/Build/20986a4044eaa8551db6be07233cc1d9.js:4:5529)
    at i.callback (URL/WebGLBuild/Build/20986a4044eaa8551db6be07233cc1d9.js:3:19246)
printErr @ 20986a4044eaa8551db6be07233cc1d9.js:4
20986a4044eaa8551db6be07233cc1d9.js:3 Invoking error handler due to
Uncaught TypeError: Cannot read properties of undefined (reading 'getParameter')
97db2fc4-0736-4197-9934-0b244079d6ff:8 Uncaught TypeError: Cannot read properties of undefined (reading 'getParameter')
    at _glGetString (97db2fc4-0736-4197-9934-0b244079d6ff:8:293859)
    at 06ea8e66:0x28d47a
    at 06ea8e66:0x28ae43
    at 06ea8e66:0x288c6f
    at 06ea8e66:0x458a54
    at 06ea8e66:0x450ef8
    at 06ea8e66:0x44fce2
    at 06ea8e66:0x44dbcb
    at 06ea8e66:0x43b181
    at 06ea8e66:0x43ad55
    at Object._main (97db2fc4-0736-4197-9934-0b244079d6ff:8:495337)
    at Object.callMain (97db2fc4-0736-4197-9934-0b244079d6ff:8:542024)
    at doRun (97db2fc4-0736-4197-9934-0b244079d6ff:8:542700)
    at run (97db2fc4-0736-4197-9934-0b244079d6ff:8:542868)
    at runCaller (97db2fc4-0736-4197-9934-0b244079d6ff:8:541643)
    at Object.removeRunDependency (97db2fc4-0736-4197-9934-0b244079d6ff:8:21768)
    at processDataJob (20986a4044eaa8551db6be07233cc1d9.js:4:5529)
    at i.callback (20986a4044eaa8551db6be07233cc1d9.js:3:19246)

Does anyone have encountered a similar issue or has an idea how we can prevent webgl context loss from happening?

I would appreciate any help and hints. Thanks in advance!

EDIT: It seems to occur mostly when he is switching tabs.

Context loss happens with any WebGL build and is not related specifically to Unity.
I couldn’t find any details about this issue and fix it without reloading web page.
Most likely this is caused by a limitation on the number of contexts in a browser or the memory for them.