Recover from WebGL context loss on Safari and other browsers#225
Merged
Conversation
The postprocessing EffectComposer reads `renderer.getContext().getContextAttributes().alpha` when its renderer or passes are set. When the browser drops the WebGL context — common on Safari/WebKit under GPU pressure, with many live contexts, or on tab restore — `getContextAttributes()` returns null and the access throws "null is not an object", crashing the viewport. Add a useWebGLContextLost hook that tracks webglcontextlost/restored on the R3F canvas (calling preventDefault so the browser attempts restoration), and gate the EffectComposer on it so post-processing tears down while the context is gone and remounts cleanly on restore. Nudge a repaint on restore since the viewport uses a demand frameloop. https://claude.ai/code/session_019HGPK77WwjeXuD7wFdNF1W
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixed a viewport crash that occurs when the browser drops the WebGL context, which is especially common on Safari under GPU memory pressure, when too many live contexts exist, or when a tab is backgrounded and restored. The post-processing pipeline now gracefully tears down while the context is lost and remounts cleanly once the browser restores it.
Changes
New hook:
useWebGLContextLost— Tracks whether the viewport's WebGL context is currently lost by listening towebglcontextlostandwebglcontextrestoredevents. CallspreventDefault()on context loss to signal intent to recover, which triggers the matching restore event. Handles edge cases like contexts lost before listeners attach andgetContext()throwing on half-torn-down renderers.Updated
ViewportContent— Conditionally unmounts the post-processing EffectComposer while the context is lost (it crashes readingrenderer.getContext().getContextAttributes().alphaoff a dead context). Triggers a repaint viainvalidate()when the context is restored, since the viewport runs on demand frameloop and would otherwise sit blank until the next user interaction.Changelog entry — Documents the fix for version 0.9.4.
Implementation Details
The hook seeds initial state by checking
gl.isContextLost()in case the context was already lost before listeners attached (e.g., created on a backgrounded tab). The viewport's conditional rendering of post-processing ({engineReady && !xrPresenting && !contextLost && ...}) ensures fragile GPU-dependent subtrees are safely unmounted during context loss and remounted once Three.js's WebGLRenderer re-initializes its GL state on restore.https://claude.ai/code/session_019HGPK77WwjeXuD7wFdNF1W