From 37bb93d1818eeda0d64c02cb772c8dee5596194f Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 28 Sep 2021 14:36:19 +0100 Subject: Fix exception responding to request that has been closed (#10932) Introduced in #10905 --- changelog.d/10932.feature | 1 + synapse/http/server.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 changelog.d/10932.feature diff --git a/changelog.d/10932.feature b/changelog.d/10932.feature new file mode 100644 index 0000000000..07e7b2c6a7 --- /dev/null +++ b/changelog.d/10932.feature @@ -0,0 +1 @@ +Speed up responding with large JSON objects to requests. diff --git a/synapse/http/server.py b/synapse/http/server.py index 1a50305dcf..0df1bfbeef 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -561,9 +561,17 @@ class _ByteProducer: self._iterator = iterator self._paused = False - # Register the producer and start producing data. - self._request.registerProducer(self, True) - self.resumeProducing() + try: + self._request.registerProducer(self, True) + except RuntimeError as e: + logger.info("Connection disconnected before response was written: %r", e) + + # We drop our references to data we'll not use. + self._request = None + self._iterator = iter(()) + else: + # Start producing if `registerProducer` was successful + self.resumeProducing() def _send_data(self, data: List[bytes]) -> None: """ -- cgit 1.4.1