[fix] Ensure cleanup function is only called when instance removed#12874
Merged
sfc-gh-bnisco merged 1 commit intodevelopfrom Oct 28, 2025
Merged
[fix] Ensure cleanup function is only called when instance removed#12874sfc-gh-bnisco merged 1 commit intodevelopfrom
sfc-gh-bnisco merged 1 commit intodevelopfrom
Conversation
Contributor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Contributor
✅ PR preview is ready!
|
Contributor
There was a problem hiding this comment.
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 |
frontend/lib/src/components/widgets/BidiComponent/hooks/useHandleJsContent.ts
Show resolved
Hide resolved
1486d7a to
de5c9dd
Compare
sfc-gh-nbellante
approved these changes
Oct 27, 2025
Contributor
sfc-gh-nbellante
left a comment
There was a problem hiding this comment.
Seems reasonable to me! 👍
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
Problem:
cleanupfar too often, leading to unnecessary full component re-renders.Solution:
GitHub Issue Link (if applicable)
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.