4949 overload ,
5050)
5151
52+ import secrets
53+ import string
5254import attr
5355from typing_extensions import ParamSpec
5456from traceback import format_stack
@@ -386,6 +388,10 @@ def __enter__(self) -> "LoggingContext":
386388 self .previous_context ,
387389 format_stack ()[- 2 ]
388390 .replace ("/home/eric/Documents/github/element/synapse/" , "" )
391+ .replace (
392+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
393+ "" ,
394+ )
389395 .replace ("\n " , "->" ),
390396 )
391397 old_context = set_current_context (self )
@@ -416,6 +422,10 @@ def __exit__(
416422 self .previous_context ,
417423 format_stack ()[- 2 ]
418424 .replace ("/home/eric/Documents/github/element/synapse/" , "" )
425+ .replace (
426+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
427+ "" ,
428+ )
419429 .replace ("\n " , "->" ),
420430 )
421431 current = set_current_context (self .previous_context )
@@ -635,25 +645,39 @@ def filter(self, record: logging.LogRecord) -> Literal[True]:
635645 return True
636646
637647
648+ def random_string (length : int ) -> str :
649+ """Generate a cryptographically secure string of random letters.
650+
651+ Drawn from the characters: `a-z` and `A-Z`
652+ """
653+ return "" .join (secrets .choice (string .ascii_letters ) for _ in range (length ))
654+
655+
638656class PreserveLoggingContext :
639657 """Context manager which replaces the logging context
640658
641659 The previous logging context is restored on exit."""
642660
643- __slots__ = ["_old_context" , "_new_context" ]
661+ __slots__ = ["_old_context" , "_new_context" , "_instance_id" ]
644662
645663 def __init__ (
646664 self , new_context : LoggingContextOrSentinel = SENTINEL_CONTEXT
647665 ) -> None :
648666 self ._new_context = new_context
667+ self ._instance_id = random_string (5 )
649668
650669 def __enter__ (self ) -> None :
651670 logger .debug (
652- "PreserveLoggingContext(%s) enter (old_context=%s) (source: %s)" ,
671+ "PreserveLoggingContext(%s) %s enter (old_context=%s) (source: %s)" ,
653672 self ._new_context ,
673+ self ._instance_id ,
654674 current_context (),
655675 format_stack ()[- 2 ]
656676 .replace ("/home/eric/Documents/github/element/synapse/" , "" )
677+ .replace (
678+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
679+ "" ,
680+ )
657681 .replace ("\n " , "->" ),
658682 )
659683 self ._old_context = set_current_context (self ._new_context )
@@ -665,11 +689,16 @@ def __exit__(
665689 traceback : Optional [TracebackType ],
666690 ) -> None :
667691 logger .debug (
668- "PreserveLoggingContext(%s) exit returning to old_context=%s (source: %s)" ,
692+ "PreserveLoggingContext(%s) %s exit returning to old_context=%s (source: %s)" ,
669693 self ._new_context ,
694+ self ._instance_id ,
670695 self ._old_context ,
671696 format_stack ()[- 2 ]
672697 .replace ("/home/eric/Documents/github/element/synapse/" , "" )
698+ .replace (
699+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
700+ "" ,
701+ )
673702 .replace ("\n " , "->" ),
674703 )
675704 context = set_current_context (self ._old_context )
@@ -715,7 +744,16 @@ def set_current_context(context: LoggingContextOrSentinel) -> LoggingContextOrSe
715744 current = current_context ()
716745
717746 logger .debug (
718- "set_current_context(%s) old_context=%s - %s" , context , current , format_stack ()
747+ "| set_current_context(%s) old_context=%s - %s" ,
748+ context ,
749+ current ,
750+ [
751+ x .replace ("/home/eric/Documents/github/element/synapse/" , "" ).replace (
752+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
753+ "" ,
754+ )
755+ for x in format_stack ()
756+ ],
719757 )
720758
721759 if current is not context :
@@ -837,7 +875,25 @@ def run_in_background(
837875 CRITICAL error about an unhandled error will be logged without much
838876 indication about where it came from.
839877 """
878+ instance_id = random_string (5 )
879+ stack = (
880+ format_stack ()[- 2 ]
881+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
882+ .replace (
883+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
884+ "" ,
885+ )
886+ .replace ("\n " , "->" )
887+ )
888+
840889 current = current_context ()
890+ logger .info (
891+ "asdf run_in_background1 %s start context=%s - %s" ,
892+ instance_id ,
893+ current ,
894+ stack ,
895+ )
896+
841897 try :
842898 res = f (* args , ** kwargs )
843899 except Exception :
@@ -868,6 +924,12 @@ def run_in_background(
868924
869925 # The function may have reset the context before returning, so
870926 # we need to restore it now.
927+ logger .info (
928+ "asdf run_in_background2 %s restore log context=%s - %s" ,
929+ instance_id ,
930+ current ,
931+ stack ,
932+ )
871933 ctx = set_current_context (current )
872934
873935 # The original context will be restored when the deferred
@@ -882,7 +944,17 @@ def run_in_background(
882944 # which is supposed to have a single entry and exit point. But
883945 # by spawning off another deferred, we are effectively
884946 # adding a new exit point.)
885- d .addBoth (_set_context_cb , ctx )
947+
948+ def _asdf (result : ResultT , context : LoggingContextOrSentinel ) -> ResultT :
949+ logger .info (
950+ "asdf run_in_background3 %s reset log context at end ctx=%s - %s" ,
951+ instance_id ,
952+ ctx ,
953+ stack ,
954+ )
955+ return _set_context_cb (result , context )
956+
957+ d .addBoth (_asdf , ctx )
886958 return d
887959
888960
@@ -901,6 +973,15 @@ def run_coroutine_in_background(
901973 do not run until called, and so calling an async function without awaiting
902974 cannot change the log contexts.
903975 """
976+ stack = (
977+ format_stack ()[- 2 ]
978+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
979+ .replace (
980+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
981+ "" ,
982+ )
983+ .replace ("\n " , "->" )
984+ )
904985
905986 current = current_context ()
906987 d = defer .ensureDeferred (coroutine )
@@ -921,7 +1002,15 @@ def run_coroutine_in_background(
9211002 # which is supposed to have a single entry and exit point. But
9221003 # by spawning off another deferred, we are effectively
9231004 # adding a new exit point.)
924- d .addBoth (_set_context_cb , ctx )
1005+ def _asdf (result : ResultT , context : LoggingContextOrSentinel ) -> ResultT :
1006+ logger .info (
1007+ "asdf run_coroutine_in_background reset log context at end ctx=%s - %s" ,
1008+ ctx ,
1009+ stack ,
1010+ )
1011+ return _set_context_cb (result , context )
1012+
1013+ d .addBoth (_asdf , ctx )
9251014 return d
9261015
9271016
@@ -940,15 +1029,56 @@ def make_deferred_yieldable(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]
9401029
9411030 (This is more-or-less the opposite operation to run_in_background.)
9421031 """
1032+ instance_id = random_string (5 )
1033+ stack = (
1034+ format_stack ()[- 2 ]
1035+ .replace ("/home/eric/Documents/github/element/synapse/" , "" )
1036+ .replace (
1037+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
1038+ "" ,
1039+ )
1040+ .replace ("\n " , "->" )
1041+ )
1042+ full_stack = [
1043+ x .replace ("/home/eric/Documents/github/element/synapse/" , "" ).replace (
1044+ "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/" ,
1045+ "" ,
1046+ )
1047+ for x in format_stack ()
1048+ ]
1049+ logger .info (
1050+ "asdf make_deferred_yieldable1 %s start - %s - full stack=%s" ,
1051+ instance_id ,
1052+ stack ,
1053+ full_stack ,
1054+ )
1055+
9431056 if deferred .called and not deferred .paused :
9441057 # it looks like this deferred is ready to run any callbacks we give it
9451058 # immediately. We may as well optimise out the logcontext faffery.
9461059 return deferred
9471060
9481061 # ok, we can't be sure that a yield won't block, so let's reset the
9491062 # logcontext, and add a callback to the deferred to restore it.
1063+ logger .info (
1064+ "asdf make_deferred_yieldable2 %s reset log context - %s - full stack=%s" ,
1065+ instance_id ,
1066+ stack ,
1067+ full_stack ,
1068+ )
9501069 prev_context = set_current_context (SENTINEL_CONTEXT )
951- deferred .addBoth (_set_context_cb , prev_context )
1070+
1071+ def _asdf (result : ResultT , context : LoggingContextOrSentinel ) -> ResultT :
1072+ logger .info (
1073+ "asdf make_deferred_yieldable3 %s restore log context at end ctx=%s - %s - full stack=%s" ,
1074+ instance_id ,
1075+ prev_context ,
1076+ stack ,
1077+ full_stack ,
1078+ )
1079+ return _set_context_cb (result , context )
1080+
1081+ deferred .addBoth (_asdf , prev_context )
9521082 return deferred
9531083
9541084
0 commit comments