# CONTEXT\_LOST\_WEBGL in iOS 14.0

**URL:** <https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549>\
**Category:** Questions\
**Created:** [September 30, 2020, 3:44pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549 "2020-09-30T15:44:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [September 30, 2020, 3:44pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/1 "2020-09-30T15:44:24Z")

</div>

For the last week or two we’ve been getting reports of iOS issues, with the context being lost suddenly, and then being unable to recover. I was able to reproduce it a few times on ios 14.0, WebGL 1 and 2 on BabylonJS 4.1. I can’t say for sure it is related, but we didn’t get these reports previously, and I’ve certainly never noticed it myself.

I’ve seen after this error occurs, the context eventually restores (which I suspected after reviewing the ThinEngine source), but our scene is unusable because all of the textures etc. come from another context. Is there some way we’re supposed to be handling these gracefully and the loss of context is a standard occurrence now? Is there something we’re doing wrong that is causing this to happen, or is this just something in ios 14.0 and we should wait for ios 14.2 or perhaps a move to Babylon 4.2 when that releases?

 ![Screen Shot 2020-09-28 at 10.42.49 AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/1b08683712543a2391cc76bb544fef359b46cada.png)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [September 30, 2020, 4:26pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/2 "2020-09-30T16:26:39Z")

</div>

This is sad but safari is a pile of crappy shit.  
That being said, the engine will raise an event for you to recreate your own assets with `onContextRestoredObservable` and `onContextLostObservable`

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [September 30, 2020, 5:08pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/3 "2020-09-30T17:08:02Z")

</div>

Are there any docs or examples on how to handle this? I wasn’t able to find any yesterday.

Do you have to go all the way to making a new engine and scene, or just repopulate the scene?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [September 30, 2020, 5:14pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/4 "2020-09-30T17:14:41Z")

</div>

by default the entire scene and textures and everything should be recreated for you. Only stuff that you created by yourself will be on you

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [October 2, 2020, 2:44pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/5 "2020-10-02T14:44:43Z")

</div>

Updates: I’ve found that this issue only occurs using WebGL1, and goes away if you enable WebGL2. It also seems triggered by scrolling the underlying page or rotating the screen.

Eventually the context does restore, but my app becomes unusable as I cannot compile any shaders - none of the extensions such as derivatives work on the restored context.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 2, 2020, 4:22pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/6 "2020-10-02T16:22:48Z")

</div>

Does the option to stick with webgl2 viable for you?

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [October 2, 2020, 5:54pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/7 "2020-10-02T17:54:43Z")

</div>

It’s not really my choice - I can tell customer support to tell our customers to turn that on, but it being under the Experimental Features section of the settings makes that a bit hard to swallow.

I’m wondering if there’s something I can do to prevent it, and I’m still unsure how to recover from it when it does occur.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 2, 2020, 6:15pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/8 "2020-10-02T18:15:50Z")

</div>

Ok gotcha  
would you like then to try to mimic as much as you can what you use in the playground? I will try to simulate context lost then using webgl1 to see if we are missing something

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [October 8, 2020, 4:04pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/9 "2020-10-08T16:04:32Z")

</div>

I’ve managed to fix this issue, but updating here in case it is relevant to the Babylon internals or future devs dealing with the same issue.

The issue here is passing a uniform with the wrong type, or changing the type between updates, and then using that uniform value in the sin() function in GLSL code. For us, this was using Number.isInteger() to determine whether to call setInt or setFloat for some runtime/config driven uniform values. If you’ve used this in the sin function with multiple data types, the context will be lost when engine.resize() is called (likely in your window resize event handler).

TL;DR don’t try to infer the type at runtime, you need to keep track of this if you have any system for generically handling uniform values.

Playground example: [https://www.babylonjs-playground.com/#Q57YXQ#31](https://www.babylonjs-playground.com/#Q57YXQ#31)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 8, 2020, 4:07pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/10 "2020-10-08T16:07:20Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Alex\_B](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alex\_B](https://forum.babylonjs.com/u/Alex_B)\
**Post date:** [October 16, 2020, 9:31pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/11 "2020-10-16T21:31:51Z")

</div>

@Deltakosh

We’ve seen this as well for users who have upgraded to Safari 14.0. Right now we’re seeing the browser crash completely on Safari 14.0 on both iOS and desktop. The issue goes away (on some users) if we enable WebGL 2.0.

From the conversations you’re seeing here, do you think there’s a fix that is possible in Babylon, or do we just have to wait for Safari to update with a patch? We’re weighing whether or not to just disable 3D for these users.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 19, 2020, 3:46pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/12 "2020-10-19T15:46:18Z")

</div>

Unfortunately we are hand tied here ☹

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [October 19, 2020, 7:41pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/13 "2020-10-19T19:41:58Z")

</div>

@Alex_B Have you checked your shaders etc. for improperly passed uniform values? You might see a WebGL warning logged in the console if it is occurring.

---

<div class="post-metadata">

**Author:** ![Alex\_B](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alex\_B](https://forum.babylonjs.com/u/Alex_B)\
**Post date:** [October 19, 2020, 10:34pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/14 "2020-10-19T22:34:04Z")

</div>

On this project, we’re not writing any custom shaders and we’re just using out-of-the-box PBR materials from GLTF

---

<div class="post-metadata">

**Author:** ![Alex\_B](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alex\_B](https://forum.babylonjs.com/u/Alex_B)\
**Post date:** [October 19, 2020, 10:49pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/15 "2020-10-19T22:49:38Z")

</div>

Over on the ThreeJS issues page, they claim to have been able to reproduce the issue without creating the WebGL instance. Just resizing canvas enough times causes the crash. They’ve logged a bug with Apple but I’m not seeing much activity on that ticket.

> <https://github.com/mrdoob/three.js/issues/20453>
>
> Describe the bug
> Three js crashes on iPad IOS 14 after several canvas resizes
> To Reproduce
> Open https://jsfiddle.net/jfbt6zxa/4/ with iPad IOS 14, press ''Run...

> **[Site using WebGL rendering crashes… | Apple Developer Forums](https://developer.apple.com/forums/thread/662251?login=true&page=1#641638022)**

---

<div class="post-metadata">

**Author:** ![jekelija](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jekelija/32/5458_2.png) [@jekelija](https://forum.babylonjs.com/u/jekelija)\
**Post date:** [October 20, 2020, 2:20pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/16 "2020-10-20T14:20:18Z")

</div>

I am also seeing this issue… @Prodigal in your error log, do you still get the WebGL: INVALID\_OPERATION: delete: object does not belong to this context errors even after fixing?

Just trying to determine what errors i should expect/not expect to see.

---

<div class="post-metadata">

**Author:** ![Prodigal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/prodigal/32/7821_2.png) [@Prodigal](https://forum.babylonjs.com/u/Prodigal)\
**Post date:** [October 20, 2020, 2:50pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/17 "2020-10-20T14:50:10Z")

</div>

I don’t - after fixing my uniform issue, I can rotate the canvas (via switching between portrait / landscape) as frequently as I want and the context is never lost.

Perhaps the root cause of my issue is different than yours, but we’re using a lot of different functionality - PBRCustomShader, ShaderMaterial, CustomProceduralTexture, with animated uniforms, etc. and I was able to get it stable (after pulling my hair out for a week and a half).

---

<div class="post-metadata">

**Author:** ![jekelija](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jekelija/32/5458_2.png) [@jekelija](https://forum.babylonjs.com/u/jekelija)\
**Post date:** [October 20, 2020, 3:50pm UTC](https://forum.babylonjs.com/t/context-lost-webgl-in-ios-14-0/14549/18 "2020-10-20T15:50:42Z")

</div>

Found it, surprisingly quickly. We use a custom shader with a 2nd UV channel. The material had data for a 2nd UV channel, but the mesh didnt have a 2nd UV channel on it… wowzers.
