fix: Memory leak on reactive pipelines#1156
Open
hoxbro wants to merge 2 commits into
Open
Conversation
Derived rx nodes registered their invalidation callbacks (_invalidate_obj/_invalidate_current) on their source parameter as strong bound-method references. Since the source is typically long-lived, this pinned every derived node — and anything it captured, such as large arrays — alive indefinitely, with no way to reclaim it. Wrap the invalidation callbacks in a weakref.WeakMethod (_WeakInvalidator) and register a finalizer to prune the watcher once the node is collected. The node now becomes garbage collectable as soon as it is no longer referenced, and the source's watcher list no longer grows without bound. This changes no observable public API behavior: while an expression is referenced it behaves exactly as before, and watch()/_watch() are untouched. Only a node that was previously unreachable-but-uncollectable (pinned solely by the source's internal watcher) is now reclaimed. Assisted-by: Claude:claude-opus-4-8
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1156 +/- ##
=======================================
Coverage 86.72% 86.73%
=======================================
Files 9 9
Lines 5304 5321 +17
=======================================
+ Hits 4600 4615 +15
- Misses 704 706 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
Derived rx nodes registered their invalidation callbacks (_invalidate_obj/_invalidate_current) on their source parameter as strong bound-method references. Since the source is typically long-lived, this pinned every derived node — and anything it captured, such as large arrays — alive indefinitely, with no way to reclaim it.
Wrap the invalidation callbacks in a weakref.WeakMethod (_WeakInvalidator) and register a finalizer to prune the watcher once the node is collected. The node now becomes garbage collectable as soon as it is no longer referenced, and the source's watcher list no longer grows without bound.
This changes no observable public API behavior: while an expression is referenced it behaves exactly as before, and watch()/_watch() are untouched. Only a node that was previously unreachable-but-uncollectable (pinned solely by the source's internal watcher) is now reclaimed.
main branch:
This branch:
AI Disclosure
Assisted-by: Claude:claude-opus-4-8
Used AI to find the problem and the solution.
Checklist