fix(Lightning, RippleGrid): cancel the render loop on cleanup - #1022
Open
noron12234 wants to merge 1 commit into
Open
fix(Lightning, RippleGrid): cancel the render loop on cleanup#1022noron12234 wants to merge 1 commit into
noron12234 wants to merge 1 commit 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.
Contributor
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.
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.
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.