Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skip callbacks with dead weakrefs while iterating over callbacks #2310

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Contributors include:
- Nathan Simpson
- Beojan Stanislaus
- Daniel Werner
- Jonas Rembser
12 changes: 10 additions & 2 deletions src/pyhf/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ def _flush(self):
self._callbacks = _callbacks

def __call__(self, *args, **kwargs):
for func, arg in self.callbacks:
for func, arg in self._callbacks:
kratsg marked this conversation as resolved.
Show resolved Hide resolved
# weakref: needs to be de-ref'd first before calling
if arg is not None:
func()(arg(), *args, **kwargs)
arg_ref = arg()
if arg_ref is not None:
func()(arg_ref, *args, **kwargs)
else:
func()(*args, **kwargs)
# Flush after calling all the callbacks, not before, as callbacks in the
# beginning of the iteration might cause new dead arg weakrefs in
# callbacks that are iterated over later.
# Checking for dead weakrefs in each iteration and flushing at the end
# avoids redundant dead weakref checking in subsequent calls.
self._flush()

def __iter__(self):
return iter(self.callbacks)
Expand Down