Skip to content

replayCanvasIntegration snapshots every canvas twice a second in every session, even unchanged and hidden ones — Safari becomes sluggish with a media player open #26232

Closed

Description

@quadrifolia

Is there an existing issue for this?

How do you use Sentry?

Sentry SaaS (sentry.io)

Which SDK are you using?

@sentry/react (reproduced below with @sentry/browser)

SDK Version

11.11.0

Framework Version

React 19

Reproduction Example/SDK Setup

One HTML file, no build step. The page draws one canvas once on load and never touches it again; it counts the calls to createImageBitmap on window. Switches: ?canvas=0 (without replayCanvasIntegration), ?hidden=1 (the canvas inside a display: none element).

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>replayCanvasIntegration: a static canvas is copied forever</title></head>
<body>
<div id="player"><canvas style="width: 562px; height: 32px"></canvas></div>
<pre id="out">starting…</pre>
<script type="module">
import * as Sentry from 'https://esm.sh/@sentry/[email protected]';

const params = new URLSearchParams(location.search);
const withCanvas = params.get('canvas') !== '0';
const hidden = params.get('hidden') === '1';

// count what the page itself never does after load
let bitmaps = 0;
const createImageBitmap = window.createImageBitmap.bind(window);
window.createImageBitmap = (...args) => {
    bitmaps += 1;
    return createImageBitmap(...args);
};

// a static waveform, like an audio player's, drawn once
const canvas = document.querySelector('canvas');
const dpr = window.devicePixelRatio || 1;
canvas.width = 562 * dpr;
canvas.height = 32 * dpr;
const context = canvas.getContext('2d');
context.scale(dpr, dpr);
context.fillStyle = '#4a7';
for (let x = 0; x < 562; x += 4) {
    const height = 4 + Math.abs(Math.sin(x / 17) * Math.cos(x / 41)) * 26;
    context.fillRect(x, (32 - height) / 2, 2, height);
}
if (hidden) document.querySelector('#player').style.display = 'none';

Sentry.init({
    dsn: 'https://[email protected]/0', // nothing is sent unless an error is captured
    integrations: [Sentry.replayIntegration(), ...(withCanvas ? [Sentry.replayCanvasIntegration()] : [])],
    replaysSessionSampleRate: 0,
    replaysOnErrorSampleRate: 1.0,
});

const started = performance.now();
setInterval(() => {
    const seconds = (performance.now() - started) / 1000;
    document.querySelector('#out').textContent =
        `${withCanvas ? 'with' : 'without'} replayCanvasIntegration, canvas ${hidden ? 'hidden' : 'visible'}, unchanged since load\n` +
        `after ${seconds.toFixed(0)} s: createImageBitmap called ${bitmaps} times (${(bitmaps / seconds).toFixed(1)}/s)`;
}, 1000);
</script>
</body>
</html>

Steps to Reproduce

  1. Serve the file from any local server (module scripts do not load from file://) and open it.
  2. Watch the counter; repeat with ?hidden=1 and ?canvas=0.
  3. In Safari: open the Web Inspector, tab "Graphics". Offscreen canvases with a "Bitmap Renderer" context, each a copy of the page's canvas, keep appearing, and the inspector shows a stream of blob: snapshots.

Expected Result

With replaysSessionSampleRate: 0 and replays only on error, a canvas that has not changed since load, or one that is not even displayed, should not be copied over and over. At most it should be captured when it changes, or when a replay is actually sent.

Actual Result

Measured in Chromium 158 (Playwright), 10 s per variant, requests to sentry.io blocked (none were made):

Variant createImageBitmap calls in 10 s
default (canvas visible, unchanged) 20 (2/s)
?hidden=1 (canvas in a display: none element) 20 (2/s)
?canvas=0 (no replayCanvasIntegration) 0

So in every session — because replaysOnErrorSampleRate keeps a rolling buffer — every canvas on the page is copied with createImageBitmap, sent to the worker and encoded to WebP twice a second (the medium default), whether it changed or is visible at all.

In production this hit our CMS: a media dialog with an audio/video player whose waveform is a 1124 × 64 canvas. With the dialog open for a few minutes Safari became noticeably sluggish, and the Graphics tab listed more than a hundred offscreen bitmap-renderer copies of the waveform (Canvas 107 and counting). Removing replayCanvasIntegration() made it go away; locally, where Sentry is not initialised, it never happened.

From reading @sentry/replay-canvas 11.11.0 (build/npm/esm/index.js):

  • getCanvasElements() takes every canvas in the document (and shadow roots) that is not blocked by a selector; takeSnapshot() skips one only when its width or height is 0. There is no check for visibility, for being in the viewport, or for whether the canvas changed since the last snapshot.
  • startRAFTimestamping() and startPendingCanvasMutationFlusher() each re-register a requestAnimationFrame callback on every frame for the lifetime of the page, so the page never idles.

Suggestion

  • Snapshot a 2D canvas only when it was drawn to since the last snapshot (the package already has a 2D mutation observer that patches the drawing calls, used when sampling is off), and skip canvases that are not rendered (checkVisibility(), zero client rects) or outside the viewport.
  • Stop the per-frame loops while nothing is pending.
  • Document the cost: as it stands, replayCanvasIntegration with on-error replays costs CPU in every session, not only in the sessions that send a replay.

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions