Repository navigation
[web] Recover CanvasKit surfaces from WebGL failures without per-frame null crashes - #196257
Merged
auto-submit[bot] merged 10 commits intoNov 17, 2026
Conversation
…nd context-loss assert
harryterkelsen
marked this pull request as ready for review
November 10, 2026 00:40
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors the CanvasKit surface implementation to improve WebGL context loss recovery and software fallback handling, ensuring resources are properly released and the canvas is replaced when switching to software rendering. It also adds a comprehensive suite of regression tests. The review feedback identifies a redundant canvas release in _replaceCanvasWithSize and suggests throwing an error if MakeOnScreenGLSurface returns null to guarantee that the software fallback is correctly triggered.
_replaceCanvasWithSize called _releaseCanvasAndContext() and then _replaceCanvas(), which calls _releaseCanvas() again on the same, already-released canvas. Release it once via _releaseCanvas() and then acquire and initialize the new canvas directly.
mdebbar
previously approved these changes
Nov 12, 2026
…ter-flutter-162868-occasional-bursts-of-null
mdebbar
approved these changes
Nov 12, 2026
This was referenced Nov 18, 2026
LouiseHsu
pushed a commit
to flutter/packages
that referenced
this pull request
Nov 18, 2026
flutter/flutter@2f09f96...06f32f8 2026-11-18 [email protected] Move macOS desktop builds to ThinLTO (flutter/flutter#196804) 2026-11-18 [email protected] Roll Dart SDK from 5ed55b4b2ebc to 753488ede427 (2 revisions) (flutter/flutter#196893) 2026-11-18 [email protected] Roll Packages from 9af69be to dca2e58 (7 revisions) (flutter/flutter#196892) 2026-11-18 [email protected] Roll Skia from 503a380ebecc to 81ee7327e81c (29 revisions) (flutter/flutter#196889) 2026-11-18 [email protected] Roll Dart SDK from 26e38955d9d4 to 5ed55b4b2ebc (4 revisions) (flutter/flutter#196843) 2026-11-18 [email protected] Fix permissions for action updates (flutter/flutter#196791) 2026-11-18 [email protected] iOS: Apply platform view mutators from FlutterPlatformViewLayer (flutter/flutter#196753) 2026-11-18 [email protected] [flutter_tools] Remove ambient Doctor, FeatureFlags, and device getters (flutter/flutter#196572) 2026-11-18 [email protected] Fix RelativeRect.lerp crash when b is null (flutter/flutter#196655) 2026-11-18 [email protected] [web] Harden WebP and GIF header readers against truncated and malformed buffers (flutter/flutter#196075) 2026-11-17 [email protected] Enable apps to control focus behavior during app lifecycle changes via `FocusLifecyclePolicy` (flutter/flutter#190570) 2026-11-17 [email protected] Don't re-run the code freeze check on label changes (flutter/flutter#196745) 2026-11-17 [email protected] Android: Report FlutterView as a text editor while a text input client is attached (flutter/flutter#193074) 2026-11-17 [email protected] Roll Packages from 4c6a7d3 to 9af69be (5 revisions) (flutter/flutter#196812) 2026-11-17 [email protected] [flutter_tools] Handle target VM filesystem errors in DevFS.create (flutter/flutter#196234) 2026-11-17 [email protected] [flutter_tools] Clarify analytics reporting status when on a feature branch (flutter/flutter#194496) 2026-11-17 [email protected] [web] Recover CanvasKit surfaces from WebGL failures without per-frame null crashes (flutter/flutter#196257) 2026-11-17 [email protected] [devicelab] Trigger navigation tap via Flutter Driver in service_extensions_test (flutter/flutter#194456) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
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.
When WebGL fails, CanvasKit surfaces fall back to software rendering, but they did it on the same canvas that already owned the WebGL context. Per the HTML canvas spec,
getContext('2d')returnsnullon a canvas bound to WebGL. CanvasKit's CPU surface calls that on everyflush(), so each frame threwTypeError: Cannot read properties of null (reading 'putImageData'), which dart2js reports asNull check operator used on a null value. This happens withMakeOnScreenGLSurfacereturning null on resize,MakeGrContextfailing, or a lost context. Separately, ifrecreateContextForCanvasreceived a canvas whose context was already lost,MakeGrContextthrew (null.rangeMininside CanvasKit). That error escaped context recreation and left the surface with noSkSurface.This PR makes
CkSurface(onscreen and offscreen) recover cleanly:GrContext, and switches to a fresh canvas. This happens once; later resizes reuse the fresh canvas.MakeGrContext, and a failed WebGL context request, now trigger the software fallback instead of escaping.position: absolutestyle is stored on the surface and reapplied to replacement canvases.initializedalways completes.handledContextLossEventcompletes after recovery, including when recovery fails, andonContextLostasserts that the surface is not already in software mode.Fixes #162868
Behavioral Changes
MakeOnScreenGLSurfacereturning null on resize) kept rendering into the canvas that owned the WebGL context, and every laterrasterizeToCanvasthrewNull check operator used on a null value/null.putImageData. After: the surface switches to a new canvas once and renders on the CPU without errors.CkSurface.canvasreturns the new canvas, andglContextbecomes-1.recreateContextForCanvaswith a canvas whose WebGL context was already lost completed with an error, andskSurfacestayednull; throughonContextLostthis was an unhandled async error. After: it completes normally and the surface renders in software mode.<canvas>inCkOnscreenSurface.hostElement(two children after a context loss), and the offscreen path's oldGrContext/ WebGL context were never freed. After: the old canvas is removed from the DOM, itswebglcontextlostlistener is removed, and itsGrContextand still-live WebGL context are freed.setIsOverlay(true)styled only the current canvas, so a replacement canvas lostposition: absolute. After: the style is reapplied whenever a canvas is attached.initializednever completed, andhandledContextLossEventcompleted before recovery started. After:initializedalways completes (logging a warning on failure),handledContextLossEventcompletes once recovery has finished or failed, andonContextLostasserts!isSoftwarein debug mode.Where to focus
engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart, mainly_recreateSoftwareSkSurface,_swapCanvasForSoftwareRendering,_releaseCanvasand_replaceCanvas. Tests are inengine/src/flutter/lib/web_ui/test/canvaskit/surface_gl_failure_test.dart._canvasHasWebGlContextis set before requesting a WebGL context on the current canvas. A software fallback swaps the canvas only when that flag is set, so the swap happens at most once per WebGL-to-software transition._failedToCreateGrContextstays set, so the surface never switches back to WebGL on the new canvas. CheckinggetContext('2d')on every frame was rejected because it adds DOM interop to the hot path._releaseCanvas:CanvasProvider.releaseCanvasruns before the old WebGL context is lost on purpose. Releasing the canvas removes itswebglcontextlostlistener, so losing the context explicitly does not triggeronContextLost(which now asserts!isSoftware). The context is lost only if one was actually created (_glContext > 0): callinggetContexton a canvas whose initial request failed could allocate a new WebGL context just to lose it._webGlContextOf, soCkSurfacenever runsisA<DomOffscreenCanvas>(). That check evaluatesinstanceof OffscreenCanvasand throws in browsers that do not defineOffscreenCanvas, and Safari is where this crash is reported most._replaceCanvashelper: offscreen clamp recovery andonContextLostreadsupportsWebGlandcanvasimmediately after replacing the canvas, so they call_replaceCanvasdirectly rather than going through theasyncrecreateContextForCanvaswrapper.MakeOnScreenGLSurfacereturning null now leaves that surface in software mode permanently, instead of throwing on every frame.Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.