Skip to content

fix(onboarding): clean up setTimeout timers on unmount#980

Closed
tysoncung wants to merge 1 commit intoMODSetter:mainfrom
tysoncung:fix/950-settimeout-cleanup-onboarding-tour
Closed

fix(onboarding): clean up setTimeout timers on unmount#980
tysoncung wants to merge 1 commit intoMODSetter:mainfrom
tysoncung:fix/950-settimeout-cleanup-onboarding-tour

Conversation

@tysoncung
Copy link

@tysoncung tysoncung commented Mar 25, 2026

Problem

The onboarding tour component has two setTimeout patterns that can fire after component unmount, potentially causing state updates on unmounted components:

  1. checkAndStartTour recursion (line ~575): Uses recursive setTimeout to retry finding target DOM elements, but only the initial setTimeout is cleaned up in the effect's teardown. Inner retries keep firing after unmount.

  2. updateTarget retry (line ~480): Schedules a retry setTimeout that is never tracked or cancelled.

Solution

  1. Added a cancelled flag that's set to true in the cleanup function. The recursive checkAndStartTour checks this flag before executing, preventing state updates after unmount.

  2. Added a retryTimerRef to track the retry timer in updateTarget. The timer is cleared before scheduling a new one, preventing stale callbacks.

Changes

  • surfsense_web/components/onboarding-tour.tsx:
    • Added cancelled guard to checkAndStartTour recursion
    • Added retryTimerRef to track and clean up updateTarget retry timer

Closes #950

High-level PR Summary

This PR fixes memory leaks in the onboarding tour component by properly cleaning up setTimeout timers when the component unmounts. It introduces a cancelled flag to prevent recursive checkAndStartTour calls from executing after unmount, and adds a retryTimerRef to track and clear retry timers in the updateTarget function. These changes prevent state updates on unmounted components.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/components/onboarding-tour.tsx

Need help? Join our Discord

Analyze latest changes

Two setTimeout patterns in the onboarding tour could fire after
component unmount:

1. checkAndStartTour recursion: Only the initial setTimeout was
   cleaned up. Inner retries continued after unmount. Fixed by
   adding a cancelled flag checked before each retry.

2. updateTarget retry: The retry setTimeout was never tracked or
   cleaned. Fixed by storing it in a ref and clearing it on
   subsequent calls.

Closes MODSetter#950
@vercel
Copy link

vercel bot commented Mar 25, 2026

Someone is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

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

Review by RecurseML

🔍 Review performed on a474c46..b8f9ed0

✨ No bugs found, your code is sparkling clean

@MODSetter
Copy link
Owner

@tysoncung, please raise this PR on the "dev" branch.

@MODSetter MODSetter closed this Mar 25, 2026
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.

Fix missing setTimeout cleanup in onboarding tour retry logic

2 participants