Repository navigation
Description
Steps to reproduce
I hit this on a page that removes its Flutter views and later adds new ones to the same elements.
flutter create --platforms web --empty bug && cd bug, then replacelib/main.dartand add a newweb/flutter_bootstrap.jswith the code below.flutter build web, thenpython3 -m http.server 8000 -d build/web.- Open http://localhost:8000 in Safari, where CanvasKit uses its multi-surface rasterizer, or http://localhost:8000/?multisurface in Chrome, which sets
canvasKitForceMultiSurfaceRasterizer: true. - Press "Remove and add the view" ten times.
Tested: 3.47.7 stable, master c90776d and 3.44.9, release builds, Safari 26.5 and Chrome 155 on macOS 26.5.1, plus Firefox 155 and WebKit 26.6 through Playwright.
Expected results
The blue box keeps drawing, as it does in Chrome without ?multisurface. The embedding guide says a page can add and remove views.
Actual results
Each press adds two WebGL contexts, and the removed view keeps its two. By press 7 the page passes the browser's limit of 16, and Safari and Chrome start dropping the oldest. The removed views' surfaces rebuild a context for each one dropped, so from press 7 the counter climbs by over a hundred a second without further presses, and the box stays blank. Nothing is thrown. Firefox also adds two contexts per press and warns from press 149, when it passes its limit of 300, but it keeps drawing. Without ?multisurface, Chrome's counter stays at 3.
3.44.9 kept drawing. #180610 added the rebuild in onContextLost in 3.41.0, but a late field made it throw a LateInitializationError until #185553 in 3.47.0.
Cause
From reading 3.47.7, unchanged at master:
ViewRasterizer.disposedisposes only the embedder, not the view's base canvas or cached overlays (rasterizer.dart#L130-L132)._viewRasterizers(multi_surface_rasterizer.dart#L34),_createdSurfaces(surface.dart#L26-L35) and the canvas provider's_eventListeners(canvas_provider.dart#L16-L33) keep every removed view's objects.CkSurface.disposedeletes only theSkSurface(surface.dart#L227-L230), and nothing calls CanvasKit'sdeleteContext.- A disposed surface keeps its
webglcontextlostlistener, andonContextLostalways rebuilds (surface.dart#L104-L111).
I patched the compiled engine of a 3.47.7 profile build so that removing a view releases its surfaces. Each one removes its canvas and listener, abandons and deletes its GrDirectContext, calls deleteContext and is dropped from those three collections. Chrome then stayed under the limit for 30 presses. Safari and WebKit kept counting the released contexts until garbage collection, but the box kept drawing because nothing rebuilt them. Skipping only the rebuild also kept the box drawing in all three, but freed nothing.
Code sample
Code sample
lib/main.dart:
import 'package:flutter/widgets.dart';
void main() => runWidget(const MultiViewApp());
class MultiViewApp extends StatefulWidget {
const MultiViewApp({super.key});
@override
State<MultiViewApp> createState() => _MultiViewAppState();
}
class _MultiViewAppState extends State<MultiViewApp>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
// Called when the page adds or removes a view.
@override
void didChangeMetrics() => setState(() {});
@override
Widget build(BuildContext context) {
return ViewCollection(
views: [
for (final view in WidgetsBinding.instance.platformDispatcher.views)
View(view: view, child: const ColoredBox(color: Color(0xFF2196F3))),
],
);
}
}web/flutter_bootstrap.js:
{{flutter_js}}
{{flutter_build_config}}
// Counts the canvases that get a WebGL context.
let contexts = 0;
for (const Canvas of [HTMLCanvasElement, OffscreenCanvas]) {
const getContext = Canvas.prototype.getContext;
Canvas.prototype.getContext = function (type, ...args) {
const context = getContext.call(this, type, ...args);
if (type.startsWith('webgl') && context && !this.counted) {
this.counted = true;
contexts++;
}
return context;
};
}
const button = document.body.appendChild(document.createElement('button'));
button.textContent = 'Remove and add the view';
const count = document.body.appendChild(document.createElement('p'));
const host = document.body.appendChild(document.createElement('div'));
host.style.cssText = 'position: relative; overflow: hidden; height: 200px';
_flutter.loader.load({
onEntrypointLoaded: async (engineInitializer) => {
const engine = await engineInitializer.initializeEngine({
multiViewEnabled: true,
canvasKitForceMultiSurfaceRasterizer: location.search.includes('multisurface'),
});
const app = await engine.runApp();
let viewId = app.addView({ hostElement: host });
button.onclick = () => {
app.removeView(viewId);
viewId = app.addView({ hostElement: host });
};
setInterval(() => (count.textContent = `WebGL contexts created: ${contexts}`), 100);
},
});Screenshots or Video
No response
Logs
Logs
Console messages, captured with Playwright.
Chrome 155.0.8059.86 with ?multisurface, repeating from press 7:
WARNING: Too many active WebGL contexts. Oldest context will be lost.WebKit 26.6, repeating from press 6 or 7:
There are too many active WebGL contexts on this page, the oldest context will be lost.Firefox 155.0, once on press 149 and twice on each press after:
Exceeded 300 live WebGL contexts for this principal, losing the least recently used one.Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.47.7, on macOS 26.5.1 25F80 darwin-arm64, locale en-US) [738ms]
• Flutter version 3.47.7 on channel stable at <redacted>
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision abaf9c5237 (13 days ago), 2026-10-08 09:51:21 -0700
• Engine revision deb287481e
• Dart version 3.13.5
• DevTools version 2.60.0
• Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-record-use, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration
[✓] Android toolchain - develop for Android devices (Android SDK version 36.1.0) [1,564ms]
• Android SDK at <redacted>
• Emulator version 37.1.11.0 (build_id 15917651) (CL:N/A)
• Platform android-36.1, build-tools 36.1.0
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
This is the JDK bundled with the latest Android Studio installation on this machine.
To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
• Java version OpenJDK Runtime Environment (build 25.0.3+-15898627-b508.16)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 26.3) [1,256ms]
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 17C529
• CocoaPods version 1.16.2
[✓] Chrome - develop for the web [5ms]
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Connected device (4 available) [6.6s]
• Pixel 7 Pro (mobile) • <redacted> • android-arm64 • Android 17 (API 37)
• <redacted> (mobile) • <redacted> • ios • iOS 26.7.1 23H30
• macOS (desktop) • macos • darwin-arm64 • macOS 26.5.1 25F80 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 155.0.8059.86
[✓] Network resources [570ms]
• All expected network resources are available.
• No issues found!
Activity
mbcorona commentedon Oct 22, 2026
Hi @T-Davis, thanks for the thorough root cause and the compiled-engine validation. Reproduces on Safari here: context count climbs and the view goes blank. It holds up against current source too: dispose frees only the SkSurface, nothing calls deleteContext, and onContextLost re-registers itself on rebuild.
Routing to team-web.
[web] Release CanvasKit WebGL contexts and surfaces when a view is re…