Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix memory leaks in Flet web app#6186

Merged
FeodorFitsner merged 4 commits intomainfrom
mem-leak-fix
Feb 19, 2026
Merged

Fix memory leaks in Flet web app#6186
FeodorFitsner merged 4 commits intomainfrom
mem-leak-fix

Conversation

@FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Feb 18, 2026

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:

  • Ensure effect hooks run cleanup before setup only when dependencies change and do not schedule effects when dependencies are unchanged.
  • Release cached render state and context references when components unmount to avoid retaining unused objects.
  • Track original dataclass field values without unbounded tuple growth and correctly release previous list/dict/dataclass snapshots when fields are set to None.
  • Prevent disconnected sessions awaiting expiration from buffering incremental messages, updates, and effects indefinitely, and clear pending buffers on disconnect.

Tests:

  • Add tests covering effect hook scheduling semantics on dependency changes and verifying no scheduling when dependencies are unchanged.
  • Add tests ensuring component unmount clears cached render references and contexts.
  • Add tests validating dataclass change tracking behavior and cleanup of previous list snapshots when fields become None.
  • Add tests verifying disconnected sessions drop incremental messages and ignore scheduled updates and effects.
  • Add tests confirming renderer.with_context correctly binds and resets the current renderer and reuses its context manager type.

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.
@cloudflare-workers-and-pages
Copy link

Deploying flet-examples with  Cloudflare Pages  Cloudflare Pages

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

View logs

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We've reviewed this pull request using the Sourcery rules engine

Copy link
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 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.

@cloudflare-workers-and-pages
Copy link

Deploying flet-docs with  Cloudflare Pages  Cloudflare Pages

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

View logs

@FeodorFitsner FeodorFitsner merged commit d6461e7 into main Feb 19, 2026
63 of 99 checks passed
@FeodorFitsner FeodorFitsner deleted the mem-leak-fix branch February 19, 2026 00:50
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.

Potential memory growth / high steady-state RSS in Flet web app under route churn (object_patch.py tuple-heavy allocations)

2 participants