fix: Improve functools.partial as stream subscriber - #6970
Closed
hoxbro wants to merge 2 commits into
Closed
Conversation
`_WeakSubscriber` previously stored a weak reference to the `partial` object itself, which -- being a temporary with no other strong reference -- was collected immediately, so the subscriber never fired (#6945). Instead unwrap the partial down to the underlying bound method, hold a `WeakMethod` to it (keeping the memory-leak fix of #6934 intact) and store the partial's bound args/kwargs separately, reconstructing the call on trigger. The subscriber hash now identifies the bound method plus the bound arguments (by id) so distinct partials over the same method are not collapsed by `unique_iterator`. Assisted-by: Claude Code:claude-opus-4-8
Method subscribers are held weakly so their bound object can be garbage collected once a session ends (#6934). When the object is only reachable through the subscription -- as with a `pn.viewable.Viewer` app whose instance Panel does not retain -- it is collected immediately and the subscriber never fires (#6945). Pin a strong reference to the subscriber on the current session's document via `on_session_destroyed`, so it stays alive while the plot is displayed and is released when the session is destroyed. Outside a served session there is no document and the weak reference is used as-is. Assisted-by: Claude Code:claude-opus-4-8
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6970 +/- ##
=======================================
Coverage 89.34% 89.35%
=======================================
Files 344 344
Lines 74664 74721 +57
=======================================
+ Hits 66710 66767 +57
Misses 7954 7954 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
functools.partial as stream subscribersfunctools.partial as stream subscriber
functools.partial as stream subscriberfunctools.partial as stream subscriber
4 tasks
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
Fixes #6945
_WeakSubscribernow unwrapfunctools.partialdown to the bound method, hold aWeakMethodto it, and store the partial's args/kwargs to reapply on call.Also pin a strong reference to the subscriber on the document via
on_session_destroyedso it survives while displayed and is released when the session ends.AI Disclosure
Tool & Model: Claude Code:claude-opus-4-8
Usage: 1) Add to make an unwrap of functool.partial to remove the
weakref. 2) Make aon_session_destroyedto strong pin it.Checklist