Skip to content

Fix: Clear draggingTimeout on unmount to prevent timer leak#323

Open
upendra512 wants to merge 1 commit intoAOSSIE-Org:mainfrom
upendra512:fix/dragging-timeout-cleanup-309
Open

Fix: Clear draggingTimeout on unmount to prevent timer leak#323
upendra512 wants to merge 1 commit intoAOSSIE-Org:mainfrom
upendra512:fix/dragging-timeout-cleanup-309

Conversation

@upendra512
Copy link

@upendra512 upendra512 commented Mar 22, 2026

Description

Fixes Issue #309 - draggingTimeout not cleared on unmount in useTrackpadGesture

Changes

  • Added useEffect cleanup function to clear draggingTimeout on component unmount
  • Prevents timer memory leak and potential errors from stale references

Testing

  • ? Tested component mount and unmount
  • ? Verified timeout is properly cleared on cleanup
  • ? Confirmed no timer leaks after unmount

Technical Details

The draggingTimeout was set in handleTouchEnd (line 243) but never cleaned up when the component unmounts. This could cause:

  1. Timer continuing to run after component is unmounted
  2. Memory leak from uncancelled timer
  3. Potential errors if timeout callback accesses stale component state

The fix adds a standard React cleanup pattern with useEffect to ensure the timeout is cleared on unmount.

Related Issue

Closes #309

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where gesture-related timeout handlers could execute after component unmount, preventing potential memory leaks and unintended behavior.

Add useEffect cleanup to clear draggingTimeout when component unmounts.
Previously, the timeout could fire after unmount, causing timer memory
leak and potential errors from accessing stale references.

Fixes AOSSIE-Org#309

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 22, 2026

Walkthrough

A memory leak fix in the useTrackpadGesture hook that clears a pending timeout on component unmount. The draggingTimeout ref is now properly cleaned up via a useEffect return function to prevent the timeout handler from executing after the component is disposed.

Changes

Cohort / File(s) Summary
Memory Leak Cleanup
src/hooks/useTrackpadGesture.ts
Added useEffect cleanup on mount that clears draggingTimeout.current on unmount to prevent stale timeout execution. Updated React import to include useEffect.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested labels

Typescript Lang

Poem

🐰 A timeout lingered when the hook would fade,
So we added cleanup—a proper trade!
Now unmounted it clears without a trace,
No memory leaks in this peaceful place. ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description covers the issue, changes made, testing performed, and technical details, but does not follow the repository's description template. Use the repository's standard PR description template, including all required sections like Addressed Issues, Functional Verification checklist, and Additional Notes.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding cleanup to clear draggingTimeout on component unmount to prevent timer leaks.
Linked Issues check ✅ Passed The PR directly addresses Issue #309 by implementing the required useEffect cleanup to clear draggingTimeout on component unmount.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the timer memory leak in useTrackpadGesture hook as specified in Issue #309; no extraneous modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable poems in the walkthrough.

Disable the reviews.poem setting to disable the poems in the walkthrough.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/hooks/useTrackpadGesture.ts`:
- Around line 257-263: The cleanup clears draggingTimeout.current on unmount
correctly; for consistency with the pattern used around lines 137-138, after
calling clearTimeout(draggingTimeout.current) in the useEffect cleanup, also set
draggingTimeout.current = null so the ref is explicitly cleared before unmount;
locate the useEffect cleanup where draggingTimeout.current is conditionally
cleared and add the null assignment immediately after the clearTimeout call.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d45dd8c2-7110-4c67-8de8-b7aa78fb21ee

📥 Commits

Reviewing files that changed from the base of the PR and between 25b2382 and 13972fc.

📒 Files selected for processing (1)
  • src/hooks/useTrackpadGesture.ts

Comment on lines +257 to +263
useEffect(() => {
return () => {
if (draggingTimeout.current) {
clearTimeout(draggingTimeout.current)
}
}
}, [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Cleanup implementation is correct and addresses the memory leak.

The empty dependency array ensures the cleanup runs only on unmount, and the conditional check before clearTimeout is appropriate since draggingTimeout.current may be null.

One minor suggestion for consistency with lines 137-138: you could also null out the ref after clearing, though it's not strictly necessary since the component is unmounting.

,

♻️ Optional: Null out ref for consistency
 useEffect(() => {
   return () => {
     if (draggingTimeout.current) {
       clearTimeout(draggingTimeout.current)
+      draggingTimeout.current = null
     }
   }
 }, [])
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
return () => {
if (draggingTimeout.current) {
clearTimeout(draggingTimeout.current)
}
}
}, [])
useEffect(() => {
return () => {
if (draggingTimeout.current) {
clearTimeout(draggingTimeout.current)
draggingTimeout.current = null
}
}
}, [])
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/hooks/useTrackpadGesture.ts` around lines 257 - 263, The cleanup clears
draggingTimeout.current on unmount correctly; for consistency with the pattern
used around lines 137-138, after calling clearTimeout(draggingTimeout.current)
in the useEffect cleanup, also set draggingTimeout.current = null so the ref is
explicitly cleared before unmount; locate the useEffect cleanup where
draggingTimeout.current is conditionally cleared and add the null assignment
immediately after the clearTimeout call.

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.

[Bug]: draggingTimeout not cleared on unmount in useTrackpadGesture

2 participants