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.

request: Replace tornado_handler weak reference with tornado_handler_id.

Anders Kaseorg5033beb99c0b
diff --git a/zerver/decorator.py b/zerver/decorator.py
index ea18dbf198c..2d4ad17fe12 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -837,7 +837,7 @@ def internal_notify_view(
if not authenticate_notify(request):
raise AccessDeniedError()
request_notes = RequestNotes.get_notes(request)
- is_tornado_request = request_notes.tornado_handler is not None
+ is_tornado_request = request_notes.tornado_handler_id is not None
# These next 2 are not security checks; they are internal
# assertions to help us find bugs.
if is_tornado_view and not is_tornado_request:
diff --git a/zerver/lib/request.py b/zerver/lib/request.py
index 04eb75e65dc..b7d0a18b9a7 100644
--- a/zerver/lib/request.py
+++ b/zerver/lib/request.py
@@ -1,5 +1,4 @@
import threading
-import weakref
from collections import defaultdict
from dataclasses import dataclass, field
from functools import wraps
@@ -27,7 +26,6 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
import zerver.lib.rate_limiter as rate_limiter
-import zerver.tornado.handlers as handlers
from zerver.lib.exceptions import ErrorCode, InvalidJSONError, JsonableError
from zerver.lib.notes import BaseNotes
from zerver.lib.types import Validator, ViewFuncT
@@ -65,9 +63,7 @@ class RequestNotes(BaseNotes[HttpRequest, "RequestNotes"]):
error_format: Optional[str] = None
placeholder_open_graph_description: Optional[str] = None
saved_response: Optional[HttpResponse] = None
- # tornado_handler is a weak reference to work around a memory leak
- # in WeakKeyDictionary (https://bugs.python.org/issue44680).
- tornado_handler: Optional["weakref.ReferenceType[handlers.AsyncDjangoHandler]"] = None
+ tornado_handler_id: Optional[int] = None
processed_parameters: Set[str] = field(default_factory=set)
ignored_parameters: Set[str] = field(default_factory=set)
diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py
index 8dd47a63ef0..410dcc1a28a 100644
--- a/zerver/lib/test_helpers.py
+++ b/zerver/lib/test_helpers.py
@@ -3,7 +3,6 @@ import os
import re
import sys
import time
-import weakref
from contextlib import contextmanager
from functools import wraps
from typing import (
@@ -336,7 +335,7 @@ class HostRequestMock(HttpRequest):
RequestNotes(
client_name="",
log_data={},
- tornado_handler=None if tornado_handler is None else weakref.ref(tornado_handler),
+ tornado_handler_id=None if tornado_handler is None else tornado_handler.handler_id,
client=get_client(client_name) if client_name is not None else None,
),
)
diff --git a/zerver/tornado/handlers.py b/zerver/tornado/handlers.py
index 06d98c32113..e23f662e076 100644
--- a/zerver/tornado/handlers.py
+++ b/zerver/tornado/handlers.py
@@ -1,6 +1,5 @@
import logging
import urllib
-import weakref
from typing import Any, Dict, List
import tornado.web
@@ -111,16 +110,16 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
# `get_response()`.
set_script_prefix(get_script_name(environ))
signals.request_started.send(sender=self.__class__)
- request = WSGIRequest(environ)
+ self._request = WSGIRequest(environ)
# We do the import during runtime to avoid cyclic dependency
from zerver.lib.request import RequestNotes
# Provide a way for application code to access this handler
# given the HttpRequest object.
- RequestNotes.get_notes(request).tornado_handler = weakref.ref(self)
+ RequestNotes.get_notes(self._request).tornado_handler_id = self.handler_id
- return request
+ return self._request
async def write_django_response_as_tornado_response(self, response: HttpResponse) -> None:
# This takes a Django HttpResponse and copies its HTTP status
diff --git a/zerver/tornado/views.py b/zerver/tornado/views.py
index 9b278a42a52..731893f6919 100644
--- a/zerver/tornado/views.py
+++ b/zerver/tornado/views.py
@@ -123,11 +123,8 @@ def get_events_backend(
raise JsonableError(_("User not authorized for this query"))
# Extract the Tornado handler from the request
- tornado_handler = RequestNotes.get_notes(request).tornado_handler
- assert tornado_handler is not None
- handler = tornado_handler()
- assert handler is not None
- handler._request = request
+ handler_id = RequestNotes.get_notes(request).tornado_handler_id
+ assert handler_id is not None
if user_client is None:
valid_user_client = RequestNotes.get_notes(request).client
@@ -160,7 +157,7 @@ def get_events_backend(
last_event_id=last_event_id,
client_type_name=valid_user_client.name,
dont_block=dont_block,
- handler_id=handler.handler_id,
+ handler_id=handler_id,
new_queue_data=new_queue_data,
)
if "extra_log_data" in result: