summary refs log tree commit diff
path: root/synapse/util/async.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-06-19 11:49:58 +0100
committerErik Johnston <erik@matrix.org>2015-06-19 11:49:58 +0100
commit275dab6b5508642786e9b7ea16a8c921432e0a73 (patch)
treed6dc7103aa8639835f5f49ac7f437f81009706c1 /synapse/util/async.py
parentMerge pull request #189 from matrix-org/erikj/room_init_sync (diff)
parentAdd comment on cancellation of observers (diff)
downloadsynapse-275dab6b5508642786e9b7ea16a8c921432e0a73.tar.xz
Merge pull request #190 from matrix-org/erikj/syn-412
Fix notifier leak
Diffstat (limited to 'synapse/util/async.py')
-rw-r--r--synapse/util/async.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/synapse/util/async.py b/synapse/util/async.py
index 1c2044e5b4..5a1d545c96 100644
--- a/synapse/util/async.py
+++ b/synapse/util/async.py
@@ -38,6 +38,9 @@ class ObservableDeferred(object):
     deferred.
 
     If consumeErrors is true errors will be captured from the origin deferred.
+
+    Cancelling or otherwise resolving an observer will not affect the original
+    ObservableDeferred.
     """
 
     __slots__ = ["_deferred", "_observers", "_result"]
@@ -45,7 +48,7 @@ class ObservableDeferred(object):
     def __init__(self, deferred, consumeErrors=False):
         object.__setattr__(self, "_deferred", deferred)
         object.__setattr__(self, "_result", None)
-        object.__setattr__(self, "_observers", [])
+        object.__setattr__(self, "_observers", set())
 
         def callback(r):
             self._result = (True, r)
@@ -74,12 +77,21 @@ class ObservableDeferred(object):
     def observe(self):
         if not self._result:
             d = defer.Deferred()
-            self._observers.append(d)
+
+            def remove(r):
+                self._observers.discard(d)
+                return r
+            d.addBoth(remove)
+
+            self._observers.add(d)
             return d
         else:
             success, res = self._result
             return defer.succeed(res) if success else defer.fail(res)
 
+    def observers(self):
+        return self._observers
+
     def __getattr__(self, name):
         return getattr(self._deferred, name)