Skip to content

fix: Memory leak in useCopyToClipboard due to missing setTimeout cleanup (clipboard_44) #4227

@kiwina

Description

@kiwina

name: Bug Report
description: Missing setTimeout cleanup in useCopyToClipboard hook
labels: ["bug", "memory-leak", "guaranteed", "react", "auto-generated"]

Thanks for your report! Please check existing issues first:
👉 https://github.com/RooCodeInc/Roo-Code/issues

App Version

N/A (Static Analysis - Detected in clipboard_44)

API Provider

Not Applicable / Other

Model Used

N/A (UI Utility Hook - useCopyToClipboard)

🔁 Steps to Reproduce

  1. Setup: A React component utilizing the useCopyToClipboard hook from webview-ui/src/utils/clipboard.ts.
  2. Exact actions:
    • Call the copyWithFeedback function returned by the hook.
    • The component unmounts before the feedbackDuration (default 2000ms) elapses.
  3. What happened after each step:
    • The setTimeout scheduled at line 44 in clipboard.ts to hide the feedback message (setShowCopyFeedback(false)) fires after the component has unmounted.
    • This results in a React warning: "Can't perform a React state update on an unmounted component."

💥 Outcome Summary

Expected the setTimeout in useCopyToClipboard to be cleared if the component unmounts. However, the timeout callback attempts a state update on an unmounted component, causing a warning and potential instability.

📄 Relevant Logs or Errors (Optional)

Summary:

The useCopyToClipboard hook's copyWithFeedback function schedules a setTimeout at line 44 to hide a feedback message. This timeout is not cleared if the component using the hook unmounts before the timeout completes.

If copyWithFeedback is called and the component unmounts before the feedbackDuration (default 2000ms) elapses, the setTimeout callback will attempt to call setShowCopyFeedback(false) on an unmounted component. This leads to a React warning ("Can't perform a React state update on an unmounted component") and can cause unexpected behavior.

The issue is in webview-ui/src/utils/clipboard.ts around line 44.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue - In ProgressSomeone is actively working on this. Should link to a PR soon.bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions