editor icon indicating copy to clipboard operation
editor copied to clipboard

fix(viewer): guard 3D viewer against null WebGL context (Sentry MONOREPO-EDITOR-59)

Open anton-pascal opened this issue 5 months ago • 8 comments

Sentry MONOREPO-EDITOR-59

TypeError: Cannot read properties of null (reading 'getSupportedExtensions') — 5282 events / 0 users.

The 0-users signature is the tell: this is headless bot / crawler traffic (plus GPU-blocklisted or hardware-acceleration-disabled browsers) hitting the public /viewer/:id route.

Root cause

Stack: three.js three.webgpu.js → Backend/Renderer init → WebGPU is unavailable so three.js transparently falls back to a WebGL backend → canvas.getContext('webgl2' | 'webgl') returns null → three calls gl.getSupportedExtensions() on the null context and throws.

The viewer already has a capability gate (canMountGpuViewer, added in #403) plus an UnsupportedGpuViewerFallback. But the gate mounted the canvas whenever 'gpu' in navigator was truthy:

if (!('gpu' in navigator) && !canCreateWebGLContext()) return false

That navigator.gpu flag being present does not guarantee WebGPU can produce a device. On headless/blocklisted browsers the adapter request fails, three falls back to WebGL, and if no real WebGL context is obtainable either, renderer.init() throws on the null context — the crash above. So the "optimistic" branch let exactly the crashing population through.

The fix

Require an actually obtainable WebGL context before mounting, unconditionally:

function canMountGpuViewer() {
  if (typeof window === 'undefined') return false
  if (!canCreateWebGLContext()) return false
  return true
}

Rationale: the WebGPURenderer fallback path always needs WebGL, so a creatable WebGL context is the true, verifiable precondition for mounting. This converts the flaky "gpu-in-navigator" optimism into a hard gate:

  • Headless bots / blocklisted browsers (no creatable context) now hit the existing UnsupportedGpuViewerFallback UI instead of crashing.
  • Real WebGPU-capable browsers always expose WebGL too, so no capable device is newly excluded.

Minimal, low-risk: one predicate changed, reusing the existing canCreateWebGLContext() helper and the existing fallback UI. No renderer/init refactor.

Scope

  • Touches only packages/viewer/src/components/viewer/index.tsx (the /viewer route renderer gate).
  • Does not overlap PR #447 (glb-export.ts).

Do not merge without review.


[!NOTE] Medium Risk Changes how and when the 3D canvas mounts and how WebGPU/WebGL backends are selected; mistakes could affect real browsers or leave headless traffic in a hung init state, though behavior is covered by new tests.

Overview Adds renderer-capability (detectRendererCapability, initializeGpuRenderer) so the viewer no longer relies on a pre-mount navigator.gpu / WebGL probe gate. The R3F gl factory now runs through this helper: request a WebGPU device (with a 4s timeout), probe WebGL on a separate canvas, create WebGPURenderer with { device } or { forceWebGL: true }, and on WebGPU init failure dispose and retry on WebGL before surfacing unsupported.

Viewer drops canMountViewer / inline capability helpers; showGpuFallback is driven only by rendererInitFailed after init fails. UnsupportedGpuViewerFallback moves to its own module (copy tweak). The capability APIs are re-exported from @pascal-app/viewer, with Bun tests covering timeouts, WebGL fallback, probe isolation, and the null-context failure path tied to Sentry MONOREPO-EDITOR-59.

Reviewed by Cursor Bugbot for commit ff7c81f53cc1192e900e29326a927b34f3f8caf0. Bugbot is set up for automated code reviews on this repo. Configure here.

anton-pascal avatar Jul 01 '26 04:07 anton-pascal

Nightly Sentry triage (2026-07-11) — EDITOR-59 recurred with a new latest event (issue 7415398488) but this time from transaction /editor/:projectId (Edge 144 / Windows), not just the public /viewer/:id route. Same root cause: three.js WebGPURenderer falls back to WebGLBackend and gl.getSupportedExtensions() runs on a null GL context (WebGLBackend.init → new WebGLExtensions).

Heads up that the guard in this PR touches packages/viewer, but the editor route instantiates its own new WebGPURenderer(...) (e.g. the FloorPlanModal / editor canvas) that isn't covered here. Consider extending the null-context guard to the shared renderer-creation helper so both entry points are protected. No duplicate PR opened.

anton-pascal avatar Jul 11 '26 04:07 anton-pascal

Nightly Sentry triage 2026-07-13: MONOREPO-EDITOR-59 (Cannot read properties of null (reading 'getSupportedExtensions')) is still firing in production (latest event 2026-07-13T00:11Z, release ef1eecc6, Chrome 150/Win). Stack confirms three.webgpu.js WebGLBackend.init → new WebGLExtensions on a null GL context — exactly what this PR guards. Bumping visibility for review/merge.

anton-pascal avatar Jul 13 '26 04:07 anton-pascal

Sentry MONOREPO-EDITOR-FM (TypeError: this.gl is null, /viewer/:id, first seen 2026-07-15, 1 event / 0 users) is the same null-WebGL-context init class this PR addresses — three.js three.webgpu.js → WebGLBackend.init → new WebGLExtensions failing when the context is null (headless / GPU-blocklisted clients). No separate fix needed; this guard should cover FM too. Noting here to avoid a duplicate PR.

anton-pascal avatar Jul 15 '26 04:07 anton-pascal

Nightly Sentry triage (2026-07-17): MONOREPO-EDITOR-FM (Cannot read properties of null (reading 'getSupportedExtensions'), last seen 07-17 on /viewer/:id) is the same crash signature this PR targets (EDITOR-59, three.webgpu.js WebGLBackend.init on a null GL context). This PR's hard canCreateWebGLContext() gate should cover FM too. Flagging so we don't open a duplicate — recommend review/merge, then verify FM stops firing.

anton-pascal avatar Jul 17 '26 04:07 anton-pascal

Nightly Sentry triage (2026-07-18): MONOREPO-EDITOR-FM (Cannot read properties of null (reading 'getSupportedExtensions'), three.webgpu.js → WebGLBackend.init → new WebGLExtensions, route /viewer/:id) is the same null-WebGL-context class this PR guards against. The captured event was from a WebPageTest.org headless bot (no GPU), so it's largely crawler noise — but this PR's null-context guard should suppress it. Recommend also filtering WebPageTest/headless UAs in Sentry beforeSend. No duplicate PR opened.

anton-pascal avatar Jul 18 '26 04:07 anton-pascal

Triage result: the null-context diagnosis is credible, but this one-predicate patch is not complete enough to merge.

The follow-up Sentry events in this thread show the same crash on /editor/:projectId, while this branch only changes the Viewer mount gate. Also, requiring WebGL unconditionally assumes every usable WebGPU environment exposes WebGL; the robust contract is to centralize renderer capability/initialization and cover every canvas entry point, with the existing unsupported-GPU fallback handling init failure.

Please rebase onto current main and update this as a shared gate/fallback with regression coverage for: no navigator.gpu + no WebGL, navigator.gpu present but adapter/device unavailable, and renderer initialization failure. I’m keeping it open because the production signal is still active, but the current diff is blocked on that scope.

Aymericr avatar Jul 19 '26 15:07 Aymericr

Reworked and rebased this onto current main (1d43a39f). The old one-predicate mount guard has been replaced with a shared renderer capability/initialization contract in @pascal-app/viewer:

  • All canvas entry points: the repo has a single R3F <Canvas> / WebGPURenderer construction site in the shared <Viewer>. The editor 3D canvas (/editor/:projectId), editor preview, public viewer, and IFC viewer all mount that component, so they now go through the same gate (including the editor/FloorPlanModal concern from the review).
  • WebGPU-only environments: capability detection requests a WebGPU adapter/device first and passes the working device into WebGPURenderer; WebGL is not required when WebGPU succeeds.
  • WebGPU unavailable: if the adapter/device cannot be obtained, the helper probes an actual WebGL2 context and explicitly initializes the WebGL backend with that context.
  • Initialization failures: renderer construction and init() are caught centrally, the partial renderer is disposed, and <Viewer> switches to the existing unsupported-GPU fallback rather than rejecting into R3F.
  • Regression coverage: added cases for no WebGPU + no WebGL, WebGPU present but adapter/device unavailable (with and without WebGL fallback), WebGPU without WebGL, and renderer init() throwing.

Fresh verification after the rebase:

  • bun test: 2,498 passed, 1 skipped, 0 failed
  • bun run check-types: passed
  • bun run lint: passed
  • bun run check: passed

@Aymericr re-requesting review on the centralized implementation.

anton-pascal avatar Jul 20 '26 13:07 anton-pascal

Nightly Sentry triage 2026-07-21: this error class is still occurring on latest release 4bcc2e2 as a new issue ID — MONOREPO-EDITOR-FM Cannot read properties of null (reading 'getSupportedExtensions') (Chrome 150/Windows, 7/21 02:04 UTC). Same root cause: WebGL2 context creation returns null after WebGPU fallback. Bumping this PR — merging it should also resolve/dedupe FM.

anton-pascal avatar Jul 21 '26 04:07 anton-pascal

Reviewed this closely and pushed the fixes rather than sending it back — the underlying bug is real and the shape of the helper is right, so it wasn't worth another round trip.

Verified first, the claim I was most skeptical of: there really is only one new WebGPURenderer site in the repo, so the editor route, editor preview, public viewer and IFC viewer all genuinely flow through this gate. No 2D parity gap either — the floorplan is SVG. Merged onto current main: check-types 9/9, biome clean on 1587 files, viewer 87/87.

Two things I changed in da4e910c:

Lost GPU hint (was a blocker). The adapter was requested as requestAdapter({ featureLevel: 'compatibility' }). R3F's defaultProps supply powerPreference: 'high-performance', and three forwards that to its own requestAdapter — but only when it makes the request itself (three.webgpu.js:83922, if (parameters.device === undefined)). Since this PR hands the device in, three skips that branch entirely and the discrete-GPU hint silently disappeared. That would have traded a real perf regression across the whole user base for a 0-user crawler crash. Now threaded through from the props R3F passes the factory, so the value isn't duplicated.

Device ownership. Supplying the device makes us its owner, and three deliberately won't destroy a caller-supplied one (three.webgpu.js:86615). The renderer?.dispose?.() calls didn't cover it either, because Renderer.dispose() early-returns when _initialized === false — exactly the failed-init case. So the WebGPU→WebGL fallback path leaked the device, and a timed-out requestDevice leaked a late-resolving one against the browser's tight concurrent-device limit. Both now destroyed explicitly, with tests. Thanks @cursor for flagging this one.

Also:

  • Rewrote the PR body. It still described a superseded revision — "one predicate changed", "touches only index.tsx", "reusing the existing canCreateWebGLContext() helper" — while the diff deletes that helper and adds a new module plus tests across 5 files. Worth being careful with those; the body is what a reviewer reads first.
  • Added the reframing to the body: main already rendered the fallback (the old catch called setRendererInitFailed(true) and throw err). What kept EDITOR-59 firing was the rethrow escaping into R3F's uncaught await glConfig(...). The never-settling promise is the actual fix, so I commented in place why neither resolving nor rejecting works there — it read as an accident otherwise.
  • Dropped the two JSON.stringify(UnsupportedGpuViewerFallback()) assertions: calling a component as a plain function and stringifying the element tree passes unconditionally. Replaced with the case that actually bites — probe canvas gets a context, display canvas doesn't.

Merging once CI is green.

Aymericr avatar Aug 04 '26 18:08 Aymericr