Skip to content

Commit 3a4f696

Browse files
richvdhphil-flex
authored andcommitted
Fix "'NoneType' has no attribute start|stop" logcontext errors (matrix-org#7181)
Fixes matrix-org#7179.
1 parent e9805d7 commit 3a4f696

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

changelog.d/7181.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up some LoggingContext code.

synapse/http/site.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ def connectionLost(self, reason):
193193
self.finish_time = time.time()
194194
Request.connectionLost(self, reason)
195195

196+
if self.logcontext is None:
197+
logger.info(
198+
"Connection from %s lost before request headers were read", self.client
199+
)
200+
return
201+
196202
# we only get here if the connection to the client drops before we send
197203
# the response.
198204
#
@@ -236,13 +242,6 @@ def _started_processing(self, servlet_name):
236242
def _finished_processing(self):
237243
"""Log the completion of this request and update the metrics
238244
"""
239-
240-
if self.logcontext is None:
241-
# this can happen if the connection closed before we read the
242-
# headers (so render was never called). In that case we'll already
243-
# have logged a warning, so just bail out.
244-
return
245-
246245
usage = self.logcontext.get_resource_usage()
247246

248247
if self._processing_finished_time is None:

synapse/logging/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ def set_current_context(context: LoggingContextOrSentinel) -> LoggingContextOrSe
539539
Returns:
540540
The context that was previously active
541541
"""
542+
# everything blows up if we allow current_context to be set to None, so sanity-check
543+
# that now.
544+
if context is None:
545+
raise TypeError("'context' argument may not be None")
546+
542547
current = current_context()
543548

544549
if current is not context:

0 commit comments

Comments
 (0)