Skip to content

Commit 7938e8c

Browse files
committed
DEBUG: lost main context after request
Reproduction instructions: 1. `poetry run synapse_homeserver --config-path homeserver.yaml` 1. `curl http://localhost:8008/_matrix/client/versions` 1. Stop Synapse (`Ctrl + c`) Notice the bad log: ``` synapse.logging.context - WARNING - sentinel - LoggingContext: Expected logging context main was lost ```
1 parent 1bf7049 commit 7938e8c

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

synapse/app/homeserver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ def main() -> None:
415415
redirect_stdio_to_logs()
416416

417417
run(hs)
418+
logger.info("asdf outside main")
418419

419420

420421
if __name__ == "__main__":

synapse/http/site.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def get_authenticated_entity(self) -> Tuple[Optional[str], Optional[str]]:
299299
return None, None
300300

301301
def render(self, resrc: Resource) -> None:
302+
logger.info("asdf->SynapseRequest.render")
302303
# this is called once a Resource has been found to serve the request; in our
303304
# case the Resource in question will normally be a JsonResource.
304305

@@ -319,6 +320,11 @@ def render(self, resrc: Resource) -> None:
319320
user_agent=get_request_user_agent(self),
320321
),
321322
)
323+
logger.info(
324+
"asdf request logcontext=%s logcontext.previous_context=%s",
325+
self.logcontext,
326+
self.logcontext.previous_context,
327+
)
322328

323329
# override the Server header which is set by twisted
324330
self.setHeader("Server", self.synapse_site.server_version_string)
@@ -342,6 +348,7 @@ def render(self, resrc: Resource) -> None:
342348
servlet=self.request_metrics.name,
343349
**{SERVER_NAME_LABEL: self.our_server_name},
344350
).inc()
351+
logger.info("asdf->SynapseRequest.render done")
345352

346353
@contextlib.contextmanager
347354
def processing(self) -> Generator[None, None, None]:

synapse/logging/context.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import attr
5353
from typing_extensions import ParamSpec
54+
from traceback import format_stack
5455

5556
from twisted.internet import defer, threads
5657
from twisted.python.threadpool import ThreadPool
@@ -379,6 +380,14 @@ def set_current_context(
379380

380381
def __enter__(self) -> "LoggingContext":
381382
"""Enters this logging context into thread local storage"""
383+
logger.debug(
384+
"LoggingContext(%s) enter (previous_context=%s) (source: %s)",
385+
self,
386+
self.previous_context,
387+
format_stack()[-2]
388+
.replace("/home/eric/Documents/github/element/synapse/", "")
389+
.replace("\n", "->"),
390+
)
382391
old_context = set_current_context(self)
383392
if self.previous_context != old_context:
384393
logcontext_error(
@@ -401,13 +410,24 @@ def __exit__(
401410
Returns:
402411
None to avoid suppressing any exceptions that were thrown.
403412
"""
413+
logger.debug(
414+
"LoggingContext(%s) exit: returning to previous_context=%s (source: %s)",
415+
self,
416+
self.previous_context,
417+
format_stack()[-2]
418+
.replace("/home/eric/Documents/github/element/synapse/", "")
419+
.replace("\n", "->"),
420+
)
404421
current = set_current_context(self.previous_context)
405422
if current is not self:
406423
if current is SENTINEL_CONTEXT:
407-
logcontext_error("Expected logging context %s was lost" % (self,))
424+
logcontext_error(
425+
"LoggingContext: Expected logging context %s was lost" % (self,)
426+
)
408427
else:
409428
logcontext_error(
410-
"Expected logging context %s but found %s" % (self, current)
429+
"LoggingContext: Expected logging context %s but found %s"
430+
% (self, current)
411431
)
412432

413433
# the fact that we are here suggests that the caller thinks that everything
@@ -628,6 +648,14 @@ def __init__(
628648
self._new_context = new_context
629649

630650
def __enter__(self) -> None:
651+
logger.debug(
652+
"PreserveLoggingContext(%s) enter (old_context=%s) (source: %s)",
653+
self._new_context,
654+
current_context(),
655+
format_stack()[-2]
656+
.replace("/home/eric/Documents/github/element/synapse/", "")
657+
.replace("\n", "->"),
658+
)
631659
self._old_context = set_current_context(self._new_context)
632660

633661
def __exit__(
@@ -636,16 +664,25 @@ def __exit__(
636664
value: Optional[BaseException],
637665
traceback: Optional[TracebackType],
638666
) -> None:
667+
logger.debug(
668+
"PreserveLoggingContext(%s) exit returning to old_context=%s (source: %s)",
669+
self._new_context,
670+
self._old_context,
671+
format_stack()[-2]
672+
.replace("/home/eric/Documents/github/element/synapse/", "")
673+
.replace("\n", "->"),
674+
)
639675
context = set_current_context(self._old_context)
640676

641677
if context != self._new_context:
642678
if not context:
643679
logcontext_error(
644-
"Expected logging context %s was lost" % (self._new_context,)
680+
"PreserveLoggingContext: Expected logging context %s was lost"
681+
% (self._new_context,)
645682
)
646683
else:
647684
logcontext_error(
648-
"Expected logging context %s but found %s"
685+
"PreserveLoggingContext: Expected logging context %s but found %s"
649686
% (
650687
self._new_context,
651688
context,
@@ -677,6 +714,10 @@ def set_current_context(context: LoggingContextOrSentinel) -> LoggingContextOrSe
677714

678715
current = current_context()
679716

717+
logger.debug(
718+
"set_current_context(%s) old_context=%s - %s", context, current, format_stack()
719+
)
720+
680721
if current is not context:
681722
rusage = get_thread_resource_usage()
682723
current.stop(rusage)

0 commit comments

Comments
 (0)