Skip to content

Fix reference cycle in NotifierMixin._add_listener - #4012

Merged
embray merged 2 commits into
astropy:masterfrom
embray:fits/notifier-mixin-reference-cycle
Jul 28, 2015
Merged

embray merged 2 commits into
astropy:masterfrom
embray:fits/notifier-mixin-reference-cycle

Conversation

@embray

@embray embray commented Jul 28, 2015

Copy link
Copy Markdown
Member

NotifierMixin._add_listener had a subtle reference cycle--the inline "remove" callback contained a reference to self which is held in a __closure__. So the cycle is self._listeners ➡️
self._listeners[n] ➡️ self._listeners[n].__callback__ ➡️ self._listeners[n].__callback__.__closure__ ➡️ self.

One possible fix was to change the callback so that it only references the listeners list directly, instead of through self. But a better fix was to use a WeakValueDict instead, which also helpfully simplifies the code.

(The change in io.registry is mostly incidental--it removes an unused variable I found while hunting for reference cycles.)

This partially fixes the failing test in #4001, but it also depends on #4002 to fix reference cycles that occurred in PLY.

inline "remove" callback contained a reference to self which is
held in a __closure__.  So the cycle is self._listeners ->
self._listeners[n] -> self._listeners[n].__callback__ ->
self._listeners[n].__callback__.__closure__ -> self.

This is easily fixed by using a WeakValueDict instead, which
also helpfully simplifies the code.

(The change in io.registry is mostly incidental--it removes an
unused variable I found while hunting for reference cycles.)
@embray embray mentioned this pull request Jul 28, 2015
@embray

embray commented Jul 28, 2015

Copy link
Copy Markdown
Member Author

👐

embray added a commit that referenced this pull request Jul 28, 2015
Fix reference cycle in NotifierMixin._add_listener
@embray
embray merged commit 34ee9d4 into astropy:master Jul 28, 2015
@embray
embray deleted the fits/notifier-mixin-reference-cycle branch July 28, 2015 20:00
embray added a commit that referenced this pull request Aug 5, 2015
Fix reference cycle in NotifierMixin._add_listener
embray added a commit that referenced this pull request Aug 7, 2015
Fix reference cycle in NotifierMixin._add_listener
dhomeier pushed a commit to dhomeier/astropy that referenced this pull request Aug 11, 2015
embray added a commit that referenced this pull request Aug 11, 2015
Fix reference cycle in NotifierMixin._add_listener
embray added a commit that referenced this pull request Aug 11, 2015
Fix reference cycle in NotifierMixin._add_listener
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant