fix(Lightning, RippleGrid): cancel the render loop on cleanup - #1022
fix(Lightning, RippleGrid): cancel the render loop on cleanup#1022noron12234 wants to merge 2 commits into
Conversation
Both components run a self-scheduling requestAnimationFrame loop and never cancel it. The cleanup removes listeners (and RippleGrid even drops the WebGL context and the canvas) but the loop keeps calling into GL every frame after unmount, holding the context, program and closure state alive. Lightning is worse: its effect depends on [hue, xOffset, speed, intensity, size], so every prop change starts another loop without stopping the previous one. Captures the frame id and cancels it in the cleanup. All four variants.
There was a problem hiding this comment.
Pull request overview
This PR fixes a WebGL render-loop resource leak in the Lightning and RippleGrid backgrounds by tracking the requestAnimationFrame id and canceling it during effect cleanup, preventing render loops from continuing after unmount (and preventing loop pile-up on Lightning prop changes).
Changes:
- Store the
requestAnimationFramereturn value in ananimationFrameIdvariable. - Replace self-scheduling
requestAnimationFrame(render)calls with assignments toanimationFrameId. - Call
cancelAnimationFrame(animationFrameId)in the React effect cleanup for all four variants of both components.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/ts-tailwind/Backgrounds/RippleGrid/RippleGrid.tsx | Track/cancel the RAF loop during cleanup to stop rendering after unmount. |
| src/ts-tailwind/Backgrounds/Lightning/Lightning.tsx | Track/cancel the RAF loop during cleanup to prevent stacked loops across prop changes/unmount. |
| src/ts-default/Backgrounds/RippleGrid/RippleGrid.tsx | Same RAF cancellation fix for the TS default variant. |
| src/ts-default/Backgrounds/Lightning/Lightning.tsx | Same RAF cancellation fix for the TS default variant. |
| src/tailwind/Backgrounds/RippleGrid/RippleGrid.jsx | Same RAF cancellation fix for the Tailwind JS variant. |
| src/tailwind/Backgrounds/Lightning/Lightning.jsx | Same RAF cancellation fix for the Tailwind JS variant. |
| src/content/Backgrounds/RippleGrid/RippleGrid.jsx | Same RAF cancellation fix for the content JS variant. |
| src/content/Backgrounds/Lightning/Lightning.jsx | Same RAF cancellation fix for the content JS variant. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
public/r/*.json embeds the component source verbatim and is what the jsrepo CLI writes into a user's project, so a source-only fix still ships the uncancelled rAF loop. Regenerated with `npm run registry:build`.
|
Pushed the rebuilt registry artifacts. |
The problem
LightningandRippleGrideach run a self-schedulingrequestAnimationFrameloop and never cancel it:So the loop outlives the component.
RippleGridmakes it starker — its cleanup drops the WebGL context and removes the canvas, and the loop then keeps callingrenderer.render(...)on a context that has been deliberately lost.Lightninghas a second problem on top. Its effect depends on[hue, xOffset, speed, intensity, size], so every prop change re-runs the effect and starts another loop without stopping the previous one.Measurement
Lightningin a stock Vite React app, withrequestAnimationFramewrapped to count callbacks per second:mainhue4 timesThe middle row is the prop-change pile-up; the last row is the leak. On
mainthe component is gone from the tree and five render loops are still driving WebGL every frame.RippleGridhas the identical loop shape and cleanup omission — I verified that by reading all four variants, but I could not get it to initialise inside my minimal probe harness, so I am not claiming a measured number for it.The change
Capture the frame id and cancel it in the cleanup:
Nothing else changes. On
Lightningthis also means a prop change now cancels the old loop before the effect re-runs, which is what the existing cleanup was always supposed to do.Scope
Both components, all four variants — 8 files, +32/-16.
Verification
npx tsc --noEmit: no errors mentioning either component.npx prettier --checkclean on all eight files.npx vite buildpasses.Found by scanning effects that create a resource without releasing it in the same effect. Three more components (
SplashCursor,FallingText,Crosshair) show the same shape; I left them out of this PR so the diff stays to the two I could reason about fully, and can follow up if you want them.