feat(loop): add handleLoopError escape hatch; never crash the render loop#115
Merged
Conversation
`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>
7921514 to
a3afcaf
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
handleLoopErrorcallback to observe them.The render loop runs client-supplied code every frame — synchronous event subscribers (
frameTick,idle, and the queued events drained byflushFrameEvents) and animation steps. If any of these throw, the exception would otherwise propagate out of therequestAnimationFramecallback and permanently stop the loop — the whole app freezes until reload.What changed
WebPlatform.startLoop— wraps the frame body intry/catchand always keeps the loop alive. A throw is routed tohandleLoopErrorand the loop reschedules.RendererMainSettings— adds optionalhandleLoopError?: (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 toStageOptionsand read in the loop viastage.options.handleLoopError.Behaviour
handleLoopErrorThe loop never rethrows — a single bad frame can't freeze the app.
Divergence from upstream
Platformis a thin abstract class (nosettings/PlatformSettings), so upstream's settings-plumbing refactor was intentionally not ported — only the resilience feature.scheduledflag 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/flushFrameEventsemits.Usage
Test plan
WebPlatform.loopError.test.tscovers: handled active-frame error + reschedule, default (no handler) swallows error + loop survives, idle-path double-schedule guard, and a clean frame.tsc --buildclean; changed files lint-clean.🤖 Generated with Claude Code