Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

tornado: Handle the handler having been cleared by connection close.

Alex Vandiver7e1f21236685
diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py
index 9a7452a21be..e7eb688949e 100644
--- a/zerver/tornado/event_queue.py
+++ b/zerver/tornado/event_queue.py
@@ -197,8 +197,9 @@ class ClientDescriptor:
def add_event(self, event: Mapping[str, Any]) -> None:
if self.current_handler_id is not None:
handler = get_handler_by_id(self.current_handler_id)
- assert handler._request is not None
- async_request_timer_restart(handler._request)
+ if handler is not None:
+ assert handler._request is not None
+ async_request_timer_restart(handler._request)
self.event_queue.push(event)
self.finish_current_handler()
diff --git a/zerver/tornado/handlers.py b/zerver/tornado/handlers.py
index 70180939f5d..82264397481 100644
--- a/zerver/tornado/handlers.py
+++ b/zerver/tornado/handlers.py
@@ -24,8 +24,8 @@ handlers: Dict[int, "AsyncDjangoHandler"] = {}
fake_wsgi_container = WSGIContainer(lambda environ, start_response: [])
-def get_handler_by_id(handler_id: int) -> "AsyncDjangoHandler":
- return handlers[handler_id]
+def get_handler_by_id(handler_id: int) -> Optional["AsyncDjangoHandler"]:
+ return handlers.get(handler_id)
def allocate_handler_id(handler: "AsyncDjangoHandler") -> int:
@@ -52,12 +52,18 @@ def finish_handler(handler_id: int, event_queue_id: str, contents: List[Dict[str
from zerver.lib.request import RequestNotes
from zerver.middleware import async_request_timer_restart
- # We call async_request_timer_restart here in case we are
- # being finished without any events (because another
- # get_events request has supplanted this request)
+ # The request handler may have been GC'd by a
+ # on_connection_close elsewhere already, so we have to check
+ # it is still around.
handler = get_handler_by_id(handler_id)
+ if handler is None:
+ return
request = handler._request
assert request is not None
+
+ # We call async_request_timer_restart here in case we are
+ # being finished without any events (because another
+ # get_events request has supplanted this request)
async_request_timer_restart(request)
log_data = RequestNotes.get_notes(request).log_data
assert log_data is not None