Skip to content

[fix] Ensure cleanup function is only called when instance removed#12874

Merged
sfc-gh-bnisco merged 1 commit intodevelopfrom
fix-ccv2-cleanup
Oct 28, 2025
Merged

[fix] Ensure cleanup function is only called when instance removed#12874
sfc-gh-bnisco merged 1 commit intodevelopfrom
fix-ccv2-cleanup

Conversation

@sfc-gh-bnisco
Copy link
Copy Markdown
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco commented Oct 27, 2025

Describe your changes

Problem:

  • Before this fix, we were calling cleanup far too often, leading to unnecessary full component re-renders.
  • This is especially egregious in larger custom components, especially ones that leverage a framework like React.

Solution:

  • Updates the lifecycle of cleanup to match what we do for all other elements in Streamlit.

GitHub Issue Link (if applicable)

Testing Plan

  • Adds unit tests

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@sfc-gh-bnisco sfc-gh-bnisco added security-assessment-completed impact:internal PR changes only affect internal code change:bugfix PR contains bug fix implementation labels Oct 27, 2025
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Oct 27, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Licenses 0 0 0 0 0 issues
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 27, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-12874/streamlit-1.50.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-12874.streamlit.app (☁️ Deploy here if not accessible)

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot October 27, 2025 20:15
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 an issue where the cleanup function for bidirectional components was being called too frequently, causing unnecessary full component re-renders. The fix ensures cleanup only runs when the component is actually unmounted by Streamlit, not on every prop/state change.

Key Changes:

  • Separates component lifecycle logic into two distinct useEffect hooks: one for initialization/updates and one for unmount-only cleanup
  • Uses refs to track cleanup function, script element, and unmount state across re-renders
  • Removes early cleanup execution that was incorrectly triggered on every dependency change

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
frontend/lib/src/components/widgets/BidiComponent/hooks/useHandleJsContent.ts Refactored lifecycle management to use refs and separate effects for initialization vs cleanup
frontend/lib/src/components/widgets/BidiComponent/BidiComponent.test.tsx Added test verifying cleanup only runs on unmount, not on data changes
e2e_playwright/bidi_components/session_state_interactions.py Updated component to properly manage event listeners with cleanup function

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot October 27, 2025 20:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@sfc-gh-bnisco sfc-gh-bnisco marked this pull request as ready for review October 27, 2025 22:53
Copy link
Copy Markdown
Contributor

@sfc-gh-nbellante sfc-gh-nbellante left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me! 👍

@sfc-gh-bnisco sfc-gh-bnisco merged commit 5bcbfab into develop Oct 28, 2025
37 checks passed
@sfc-gh-bnisco sfc-gh-bnisco deleted the fix-ccv2-cleanup branch October 28, 2025 15:36
github-actions bot pushed a commit that referenced this pull request Oct 28, 2025
…12874)

## Describe your changes

**Problem:**
- Before this fix, we were calling `cleanup` far too often, leading to
unnecessary full component re-renders.
- This is especially egregious in larger custom components, especially
ones that leverage a framework like React.

**Solution:**
- Updates the lifecycle of cleanup to match what we do for all other
elements in Streamlit.


## GitHub Issue Link (if applicable)

## Testing Plan

- Adds unit tests

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:internal PR changes only affect internal code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants