Merged
Conversation
Refactor object patch change-tracking to store a single original value instead of (old,new) tuples. Use getattr(dst, ...) to obtain the new value when building diffs and guard prev_lists/prev_dicts/prev_classes removal by checking the current field value on dst. Update attribute-set logic to record the original value only once and remove the entry if the field is reverted to that value. Add tests to verify original-value tracking doesn't grow tuples and that previous snapshots are released when a tracked field becomes None.
Refactor Renderer.with_context to return a reusable inner class instance (Renderer._Context) instead of creating a new local class on each call. The new _Context encapsulates binding/resetting of _CURRENT_RENDERER on __enter__/__exit__. Add tests to ensure the context manager binds and resets the current renderer and that repeated calls to with_context return the same context manager type.
Correct effect scheduling so cleanup is run (is_cleanup=True) before setup only when dependencies changed, by computing deps_changed and only scheduling cleanup/setup in that case. Improve Session disconnect handling by clearing send/pending buffers on reset and dropping incremental messages and scheduled updates/effects while a session is disconnected but not yet evicted/reconnected (expires_at set) to avoid unbounded buffering. Add tests for component effect behavior and session disconnect buffering.
Reset component cached render references in will_unmount to avoid holding onto stale objects: clear _b, _state.last_b, _state.last_args, _state.last_kwargs and _contexts. Add a test (test_will_unmount_releases_cached_render_references) to ensure these fields are cleared after unmount.
Deploying flet-examples with
|
| Latest commit: |
4ac3a56
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://72f1d13f.flet-examples.pages.dev |
| Branch Preview URL: | https://mem-leak-fix.flet-examples.pages.dev |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses critical memory leaks in Flet web applications that were causing RSS to grow and plateau at high levels under route churn and session disconnect/reconnect activity, as detailed in issue #6182. The fixes target four distinct memory leak sources identified through tracemalloc diagnostics pointing to object_patch.py and component rendering internals.
Changes:
- Fixed unbounded tuple growth in dataclass change tracking by storing only original field values instead of (old, new) tuples
- Prevented disconnected sessions from buffering messages, updates, and effects indefinitely
- Corrected effect hook scheduling to run cleanup/setup only when dependencies change
- Released cached render state and context references on component unmount
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/python/packages/flet/src/flet/controls/object_patch.py | Changed __changes to store original values instead of tuples; fixed undefined variable bug in snapshot cleanup when fields become None |
| sdk/python/packages/flet/src/flet/messaging/session.py | Added buffer clearing on disconnect and early returns to drop messages/updates/effects for disconnected sessions awaiting expiration |
| sdk/python/packages/flet/src/flet/components/component.py | Fixed effect hook scheduling to only run when deps change; added unmount cleanup of render state; refactored context manager to nested class |
| sdk/python/packages/flet/tests/test_session_disconnect_buffering.py | Added tests verifying disconnected sessions drop incremental traffic |
| sdk/python/packages/flet/tests/test_patch_dataclass.py | Added tests for change tracking without tuple growth and snapshot cleanup |
| sdk/python/packages/flet/tests/test_component_effects.py | Added tests for effect scheduling semantics and unmount cleanup |
| sdk/python/packages/flet/tests/test_component_renderer.py | Added tests for renderer context manager behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Deploying flet-docs with
|
| Latest commit: |
4ac3a56
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://197d9862.flet-docs.pages.dev |
| Branch Preview URL: | https://mem-leak-fix.flet-docs.pages.dev |
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.
Fix #6182
Summary by Sourcery
Address memory leaks and stale state in Flet web sessions, component rendering, and dataclass change tracking, and add regression tests for the new behavior.
Bug Fixes:
Tests: