Behaviour of ObservableMixin when its `off` method is called while its `fire` method is currently iterating over listeners
See original GitHub issueSuppose 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:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 inoff()
and do acallLater(0, ...)
there if it’s true (which would only affect the probably-rare case of removing a listener in a notification method).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.And it seems works as i expected