-
-
Notifications
You must be signed in to change notification settings - Fork 244
fix: Memory leak caused by zombie event listeners in useTouchMove.ts #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
- Use AbortController for all listeners to better readability and scalability
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Walkthrough本次更改将 Changes
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
src/hooks/useTouchMove.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-react". (The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following: The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@29.7.0_@types+node@22.15.14__postcss@8.5.3/node_modules/@umijs/fabric/dist/eslint.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🔇 Additional comments (3)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a memory leak caused by zombie event listeners that persisted after component unmount. The fix introduces AbortController to properly clean up all event listeners attached to both document and the ref element.
Key Changes
- Implemented
AbortControllerfor automatic event listener cleanup via thesignaloption - Replaced manual
removeEventListenercalls with a singleabortController.abort()in the cleanup function - Formatted event listener registration with multi-line options for better readability
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ref.current.addEventListener('touchstart', onProxyTouchStart, { | ||
| passive: true, | ||
| signal: abortController.signal, | ||
| }); | ||
| ref.current.addEventListener('wheel', onProxyWheel, { | ||
| passive: false, | ||
| signal: abortController.signal, | ||
| }); |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential null reference error: ref.current could be null when the effect runs or during cleanup. The code should check if ref.current exists before adding event listeners to prevent runtime errors.
Consider adding a null check:
if (ref.current) {
ref.current.addEventListener('touchstart', onProxyTouchStart, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('wheel', onProxyWheel, {
passive: false,
signal: abortController.signal,
});
}| ref.current.addEventListener('touchstart', onProxyTouchStart, { | |
| passive: true, | |
| signal: abortController.signal, | |
| }); | |
| ref.current.addEventListener('wheel', onProxyWheel, { | |
| passive: false, | |
| signal: abortController.signal, | |
| }); | |
| if (ref.current) { | |
| ref.current.addEventListener('touchstart', onProxyTouchStart, { | |
| passive: true, | |
| signal: abortController.signal, | |
| }); | |
| ref.current.addEventListener('wheel', onProxyWheel, { | |
| passive: false, | |
| signal: abortController.signal, | |
| }); | |
| } |
The code was smelly and it causes a memory leak, even if component is unmounted it will keep the whole context inside Detached Event Listener
Summary by CodeRabbit