Skip to content

feat(loop): add handleLoopError escape hatch; never crash the render loop#115

Merged
chiefcll merged 3 commits into
mainfrom
feat/handle-loop-error
Jun 25, 2026
Merged

feat(loop): add handleLoopError escape hatch; never crash the render loop#115
chiefcll merged 3 commits into
mainfrom
feat/handle-loop-error

Conversation

@chiefcll

@chiefcll chiefcll commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Adapts the idea behind upstream lightning-js/renderer#819 to this fork: a render loop that never crashes on client-code errors, plus an optional handleLoopError callback to observe them.

The render loop runs client-supplied code every frame — synchronous event subscribers (frameTick, idle, and the queued events drained by flushFrameEvents) and animation steps. If any of these throw, the exception would otherwise propagate out of the requestAnimationFrame callback and permanently stop the loop — the whole app freezes until reload.

What changed

  • WebPlatform.startLoop — wraps the frame body in try/catch and always keeps the loop alive. A throw is routed to handleLoopError and the loop reschedules.
  • RendererMainSettings — adds optional handleLoopError?: (error: unknown) => void, defaulting to a no-op. So out of the box a bad frame is swallowed and the loop survives; apps can override the handler to log/report (and optionally recover). Threaded through to StageOptions and read in the loop via stage.options.handleLoopError.

Behaviour

handleLoopError On a frame throw
not provided (default no-op) error swallowed, loop keeps running
provided handler called with the error, loop keeps running

The loop never rethrows — a single bad frame can't freeze the app.

Divergence from upstream

  • This fork's Platform is a thin abstract class (no settings/PlatformSettings), so upstream's settings-plumbing refactor was intentionally not ported — only the resilience feature.
  • Upstream rethrows when no handler is set; here the default is a no-op so the loop is crash-proof by default.
  • A scheduled flag prevents double-scheduling: the fork's idle path queues its next tick (setTimeout) before running the throwing client code, so the catch must not also queue a frame. Without this, a handler that throws every idle frame would compound into runaway frames. (Upstream's variant has this latent double-schedule.)

Notes / scope

This fork's animation-finish path is promise-based (microtask), so it surfaces as an unhandled rejection rather than an rAF crash. The real synchronous freeze vectors this guards are the frameTick/idle/flushFrameEvents emits.

Usage

const renderer = new RendererMain(
  {
    appWidth: 1920,
    appHeight: 1080,
    // optional — defaults to a no-op; the loop survives either way
    handleLoopError: (error) => {
      reportToTelemetry(error);
    },
  },
  'app',
);

Test plan

  • WebPlatform.loopError.test.ts covers: handled active-frame error + reschedule, default (no handler) swallows error + loop survives, idle-path double-schedule guard, and a clean frame.
  • Full unit suite: 267 passed (33 files), including existing WebPlatform context-loss/OOM tests.
  • tsc --build clean; changed files lint-clean.

🤖 Generated with Claude Code

chiefcll and others added 2 commits June 25, 2026 13:18
`detectPremultiplyAlphaHonored` creates a temporary WebGL context to test
whether `createImageBitmap(..., { premultiplyAlpha: 'premultiply' })` is
honored, but it only deleted the texture/framebuffer — the context itself was
left dangling until GC.

This probe runs in a microtask *after* the main render context is created, so a
leaked context here is the newest one on the page. On embedded TV browsers
(Apollo/Sunrise, Chrome 38+) the live-context budget is tiny; the lingering
probe pushes the page over the limit and the browser evicts the *oldest*
context — the live render context — after which every `createTexture()` returns
null and the engine spams "Could not create WebGL Texture" each frame.

Release the probe context immediately via `WEBGL_lose_context` on every exit
path instead of waiting for GC. The lose event fires only on the throwaway
canvas, so it does not trip the main renderer's `webglcontextlost` handler.

Only active when `premultiplyAlphaHonored: 'auto'` is set (default is `true`,
no probe).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…loop

The render loop runs client-supplied code every frame — synchronous event
subscribers (frameTick, idle, the queued events drained by flushFrameEvents)
and animation steps. A throw in any of these would propagate out of the
requestAnimationFrame callback and permanently stop the loop, freezing the
whole app until reload.

Wrap the frame body in try/catch and always keep the loop alive. Errors are
routed to a handleLoopError(error) setting that defaults to a no-op, so by
default a bad frame is swallowed and the loop never crashes. Apps can override
the handler to log/report (and optionally recover); the loop keeps running
after it returns.

A `scheduled` flag prevents double-scheduling: the idle path queues its next
tick before running the throwing client code, so the catch must not also queue
a frame — otherwise a handler that throws every idle frame would compound into
runaway frames.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chiefcll chiefcll force-pushed the feat/handle-loop-error branch from 7921514 to a3afcaf Compare June 25, 2026 19:13
@chiefcll chiefcll changed the title feat(loop): add handleLoopError escape hatch for render-loop crashes feat(loop): add handleLoopError escape hatch; never crash the render loop Jun 25, 2026
@chiefcll chiefcll merged commit a448a6e into main Jun 25, 2026
1 check passed
@chiefcll chiefcll deleted the feat/handle-loop-error branch June 25, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant