astryx icon indicating copy to clipboard operation
astryx copied to clipboard

[Bug] charts: WebGL marks stay blank after effects re-run (StrictMode, <Activity>)

Open richm-spp opened this issue 3 weeks ago • 2 comments

Description

The WebGL marks keep their canvas across effect cycles but destroy its context on cleanup, so after a cleanup-and-rerun they never draw again.

In dotGL:

  1. The canvas is created once and kept in a ref (L112-L113).
  2. The unmount cleanup calls loseContext(gl) (WEBGL_lose_context.loseContext(), webgl.ts) and nulls glRef, but not canvasRef.
  3. When the effects run again, getWebGLContext(canvas) calls canvas.getContext('webgl') on the same canvas. That returns the same, now-lost context, so the draw effect returns early at gl.isContextLost().
  4. registerContextLossHandlers is subscribed again, but a context lost through WEBGL_lose_context.loseContext() is only restored when restoreContext() is called. Nothing calls it, so webglcontextrestored never fires and the redraw epoch never bumps.

This happens whenever React runs cleanups and then re-runs effects on the same instance: always in development under <StrictMode>, and in production when an <Activity> boundary is hidden and shown again.

The same pattern is in heatmapGL and streamGL. It is also in dotGLInteractive, whose canvases React renders, so they survive the cycle too.

Suggested fix: in the cleanup, also drop the canvas (canvasRef.current = null) so the next run creates a fresh one. For the React-rendered canvases in dotGLInteractive, either remount them with a key or call restoreContext() on the extension when the effects run again.

Reproduction

import {StrictMode} from 'react';
import {Chart, dotGL} from '@astryxdesign/charts';

const data = Array.from({length: 200}, (_, i) => ({x: i, y: Math.sin(i / 10)}));

export function App() {
  return (
    <StrictMode>
      <Chart data={data} xKey="x" series={[dotGL('y', {color: '#2563eb'})]} />
    </StrictMode>
  );
}
  • Expected: 200 points.
  • Actual (development build): the plot area is empty. The same chart without <StrictMode> draws.

Found by reading the code; I haven't run it in a browser.

Astryx Version

@astryxdesign/[email protected] (code checked on main at 16fe6a8)

Environment

Any browser with WebGL; React 19 <StrictMode> (development) or React 19.2 <Activity>

richm-spp avatar Nov 15 '26 01:11 richm-spp

(Posted by Cindy's agent.)

Thanks for the precise report. A fix is up in #11816. dotGL, heatmapGL and streamGL now drop their canvas on cleanup, so a rerun mounts a fresh one with a live context. dotGLInteractive, whose canvases React keeps, restores the contexts its cleanup lost. New tests cover each mark under <StrictMode> and through an <Activity> hide and show. I'll note here when it lands.

cixzhang avatar Nov 15 '26 02:11 cixzhang

(Posted by Cindy's agent.)

The fix has landed: #11816 merged to main as a4b8609. One change from my earlier note: dotGLInteractive now also creates fresh canvases each time its effect runs, instead of restoring the lost contexts, because Chromium refuses restoreContext() there. All four marks draw again after a <StrictMode> re-run and after an <Activity> hide and show, and tests cover each case.

cixzhang avatar Nov 15 '26 17:11 cixzhang