summary refs log tree commit diff
path: root/synapse/handlers/events.py
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2015-12-14 15:09:41 +0000
committerMark Haines <mark.haines@matrix.org>2015-12-14 15:09:41 +0000
commitdbe7892e03e2e0e6a50c54109c30b22fe4194894 (patch)
treeb8f5afc4e48124ca7684fcf1331fbdcb3fb01c66 /synapse/handlers/events.py
parentMerge pull request #441 from matrix-org/markjh/fts_skip_invalid (diff)
downloadsynapse-dbe7892e03e2e0e6a50c54109c30b22fe4194894.tar.xz
Fix a race between started/stopped stream
Diffstat (limited to 'synapse/handlers/events.py')
-rw-r--r--synapse/handlers/events.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/synapse/handlers/events.py b/synapse/handlers/events.py
index fe300433e6..576d77e0e7 100644
--- a/synapse/handlers/events.py
+++ b/synapse/handlers/events.py
@@ -69,7 +69,12 @@ class EventStreamHandler(BaseHandler):
             A deferred that completes once their presence has been updated.
         """
         if user not in self._streams_per_user:
-            self._streams_per_user[user] = 0
+            # Make sure we set the streams per user to 1 here rather than
+            # setting it to zero and incrementing the value below.
+            # Otherwise this may race with stopped_stream causing the
+            # user to be erased from the map before we have a chance
+            # to increment it.
+            self._streams_per_user[user] = 1
             if user in self._stop_timer_per_user:
                 try:
                     self.clock.cancel_call_later(
@@ -79,8 +84,8 @@ class EventStreamHandler(BaseHandler):
                     logger.exception("Failed to cancel event timer")
             else:
                 yield started_user_eventstream(self.distributor, user)
-
-        self._streams_per_user[user] += 1
+        else:
+            self._streams_per_user[user] += 1
 
     def stopped_stream(self, user):
         """If there are no streams for a user this starts a timer that will