question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Behaviour of ObservableMixin when its `off` method is called while its `fire` method is currently iterating over listeners

See original GitHub issue

Suppose we have two listeners on “close” event.

import txaio
from autobahn.util import ObservableMixin

txaio.use_twisted()

class Foo(ObservableMixin):

    def __init__(self):
        self.set_valid_events(["someEvent"])

    def doSomething(self):
        self.fire("someEvent", self)


def listener1(foo):
    f.off("someEvent", listener1)
    print("listener1 called")

def listener2(foo):
    print("listener2 called")

f = Foo()

f.on("someEvent", listener1)
f.on("someEvent", listener2)

f.doSomething()

This will output:

listener1 called

This means, when ObservableMixin off method is called inside listener1, except for last listeners, then the next listeners (listener2) will never get called.

I think this is because ObservableMixin off method is called while ObservableMixin fire method is iterating over current listeners of event…

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
meejahcommented, Oct 12, 2021

fire() does indeed just “blindly” iterate over its listeners … so it’s not re-entrant (in the sense used above). An easy fix would be to iterate over a copy of the list … or detect that situation in off() and do a callLater(0, ...) there if it’s true (which would only affect the probably-rare case of removing a listener in a notification method).

1reaction
serenity-77commented, Oct 13, 2021

fire() does indeed just “blindly” iterate over its listeners … so it’s not re-entrant (in the sense used above). An easy fix would be to iterate over a copy of the list … or detect that situation in off() and do a callLater(0, ...) there if it’s true (which would only affect the probably-rare case of removing a listener in a notification method).

def listener1(foo):
    reactor.callLater(0, lambda: f.off("someEvent", listener1))
    print("listener1 called")

Using callLater indeed can solve it.

But I ended up with this quick fix, by extending ObservableMixin, which first copy all listeners and iterate over them.

import txaio
from autobahn.util import ObservableMixin as _ObservableMixin

class ObservableMixin(_ObservableMixin):

    def fire(self, event, *args, **kwargs):
        if self._listeners is None:
            return txaio.create_future(result=[])

        self._check_event(event)

        handlers = []
        for handler in self._listeners.get(event, []):
            handlers.append(handler)

        res = []

        for handler in handlers:
            future = txaio.as_future(handler, *args, **kwargs)
            res.append(future)
        if self._parent is not None:
            res.append(self._parent.fire(event, *args, **kwargs))
        d_res = txaio.gather(res, consume_exceptions=False)
        self._results[event] = d_res
        return d_res

And it seems works as i expected

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · crossbario/autobahn-python - GitHub
Behaviour of ObservableMixin when its off method is called while its fire method is currently iterating over listeners docs enhancement.
Read more >
Issues · crossbario/autobahn-python · GitHub
Behaviour of ObservableMixin when its off method is called while its fire method is currently iterating over listeners docs enhancement.
Read more >
Observable.js Source Code | Ext JS ... - modern
If that observable is already destroyed, all its listeners were cleared ... If the supplied function returns false, the event will not fire....
Read more >
MSC06-J. Do not modify the underlying collection when an ...
The behavior of an iterator is unspecified if the underlying collection is ... method to remove an element from an ArrayList while iterating...
Read more >
3. Generators and Iterators | Advanced - Python Courses eu
Method of working: A generator is called like a function. Its return value is an iterator, i.e. a generator object. The code of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found