Skip to content

Commit

Permalink
Fix dead objects being called through weakrefs
Browse files Browse the repository at this point in the history
When events (such as 'tensorlib_changed') are called, a bug occured
previously where objects (i.e. the `TensorViewer`) are already dead.
In this case an error occured when member functions where using
attributes such as `self._sorted_indices`.

This bug is fixed by catching the cases, in which the object is
invalidated by earlier callbacks.
  • Loading branch information
wernerd-cern committed Aug 31, 2023
1 parent 205eecf commit 9b1939d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/pyhf/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ def _flush(self):
self._callbacks = _callbacks

def __call__(self, *args, **kwargs):
for func, arg in self.callbacks:
for func, arg in self._callbacks:
# 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)
# We have to flush after calling all the callbacks, not before. That's
# beacause, earlier callbacks might cause new dead arg weakrefs in
# later callbacks. So we check for dead weakrefs in each iteration
# and then we flush at the end.
self._flush()

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

0 comments on commit 9b1939d

Please sign in to comment.