summary refs log tree commit diff
diff options
context:
space:
mode:
authorSean Quah <seanq@element.io>2022-04-28 15:59:08 +0100
committerSean Quah <seanq@element.io>2022-04-28 19:28:00 +0100
commit2326bbf0999e99b57dafcebd966dd9bd942c52a2 (patch)
tree594388a01de82a747ba5e3691c25e5bbe7d78a95
parentRespect the `@cancellable` flag for `DirectServe{Html,Json}Resource`s (diff)
downloadsynapse-2326bbf0999e99b57dafcebd966dd9bd942c52a2.tar.xz
Respect the `@cancellable` flag for `RestServlet`s and `BaseFederationServlet`s
Both `RestServlet`s and `BaseFederationServlet`s register their handlers
with `HttpServer.register_paths` / `JsonResource.register_paths`. Update
`JsonResource` to respect the `@cancellable` flag on handlers registered
in this way.

Although `ReplicationEndpoint` also registers itself using
`register_paths`, it does not pass the handler method that would have the
`@cancellable` flag directly, and so needs separate handling.

Signed-off-by: Sean Quah <seanq@element.io>
-rw-r--r--synapse/http/server.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/synapse/http/server.py b/synapse/http/server.py
index 7c1c1f9d49..046524f665 100644
--- a/synapse/http/server.py
+++ b/synapse/http/server.py
@@ -301,6 +301,9 @@ class HttpServer(Protocol):
         If the regex contains groups these gets passed to the callback via
         an unpacked tuple.
 
+        The callback may be marked with the `@cancellable` decorator, which will
+        cause request processing to be cancelled when clients disconnect early.
+
         Args:
             method: The HTTP method to listen to.
             path_patterns: The regex used to match requests.
@@ -531,6 +534,8 @@ class JsonResource(DirectServeJsonResource):
     async def _async_render(self, request: SynapseRequest) -> Tuple[int, Any]:
         callback, servlet_classname, group_dict = self._get_handler_for_request(request)
 
+        request.is_render_cancellable = is_method_cancellable(callback)
+
         # Make sure we have an appropriate name for this handler in prometheus
         # (rather than the default of JsonResource).
         request.request_metrics.name = servlet_classname