Skip to content

[web] LateInitializationError in CkSurface when webglcontextlost fires during acquireCanvas #184683

Description

@Ortes

Steps to reproduce

This crash was found via Sentry on a production Flutter web app (CanvasKit renderer, release mode). It has occurred 8 times across users but AFAIK seamless for users.

No manual repro steps — it appears to be a race condition triggered by browser GPU pressure or specific browser environments.

Expected results

No crash. The webglcontextlost event should be handled safely even if it fires before CkSurface constructor completes.

Actual results

LateInitializationError: Field '' has not been initialized.

(Field name is empty due to dart2js --omit-late-names in release mode.)

Stack trace (deobfuscated via source maps)

at throwUnnamedLateFieldNI (dart-sdk/lib/_internal/js_runtime/lib/late_helper.dart:31:3)
at CkSurface.get _canvas (lib/_engine/engine/canvaskit/surface.dart:81:25)
at CanvasProvider.acquireCanvas (lib/_engine/engine/compositing/canvas_provider.dart:24:7)
triggered from: OffscreenCanvas webglcontextlost event handler

Root cause analysis

In canvas_provider.dart, acquireCanvas() registers a webglcontextlost event listener on the canvas before returning it. In CkSurface's constructor, _canvas (a late field) is assigned from the return value of acquireCanvas():

_canvas = _canvasProvider.acquireCanvas(_currentSize, onContextLost: onContextLost);

If the webglcontextlost event fires synchronously or near-synchronously during canvas creation, onContextLost() is invoked while _canvas is still uninitialized, causing the LateInitializationError.

Environment

  • Flutter: 3.41.2 (stable)
  • Renderer: CanvasKit (web)
  • Build mode: Release (--release --source-maps)
  • Sentry SDK: sentry.javascript.browser.flutter 10.38.0
  • Observed browsers: Could not determine (Sentry event lacked browser tags)

Notes

This bug was elicited by AI analysis and found on Sentry in production. There is no visible impact on users AFAIK.

Metadata

Metadata

Labels

P1High-priority issues at the top of the work listc: crashStack traces logged to the consoleengineflutter/engine related. See also e: labels.platform-webWeb applications specificallyteam-webOwned by Web platform teamtriaged-webTriaged by Web platform team

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions