3838from synapse .events import EventBase , make_event_from_dict
3939from synapse .federation .federation_base import event_from_pdu_json
4040from synapse .federation .federation_client import SendJoinResult
41- from synapse .logging .context import LoggingContext , run_in_background
41+ from synapse .logging .context import (
42+ LoggingContext ,
43+ PreserveLoggingContext ,
44+ run_in_background ,
45+ )
4246from synapse .rest import admin
4347from synapse .rest .client import login , room
4448from synapse .server import HomeServer
@@ -149,11 +153,22 @@ def test_rejected_message_event_state(self) -> None:
149153 room_version ,
150154 )
151155
152- with LoggingContext ("send_rejected" ):
156+ # To avoid "metrics will be lost" errors from running this function in the
157+ # sentinel logcontext, let's setup a logcontext to use. We use
158+ # `PreserveLoggingContext` so that the logcontext doesn't finish to avoid
159+ # "Re-starting finished log context" errors as we pump in `get_success(...)`. We
160+ # use `run_in_background` so that we run the task in the given logcontext and
161+ # handle the magic behind the scenes.
162+ send_rejected_logcontext = LoggingContext ("send_rejected" )
163+ with PreserveLoggingContext (send_rejected_logcontext ):
153164 d = run_in_background (
154165 self .hs .get_federation_event_handler ().on_receive_pdu , OTHER_SERVER , ev
155166 )
156167 self .get_success (d )
168+ # Play nice and finish the logcontext
169+ with PreserveLoggingContext ():
170+ with send_rejected_logcontext :
171+ pass
157172
158173 # that should have been rejected
159174 e = self .get_success (self .store .get_event (ev .event_id , allow_rejected = True ))
@@ -203,11 +218,22 @@ def test_rejected_state_event_state(self) -> None:
203218 room_version ,
204219 )
205220
206- with LoggingContext ("send_rejected" ):
221+ # To avoid "metrics will be lost" errors from running this function in the
222+ # sentinel logcontext, let's setup a logcontext to use. We use
223+ # `PreserveLoggingContext` so that the logcontext doesn't finish to avoid
224+ # "Re-starting finished log context" errors as we pump in `get_success(...)`. We
225+ # use `run_in_background` so that we run the task in the given logcontext and
226+ # handle the magic behind the scenes.
227+ send_rejected_logcontext = LoggingContext ("send_rejected" )
228+ with PreserveLoggingContext (send_rejected_logcontext ):
207229 d = run_in_background (
208230 self .hs .get_federation_event_handler ().on_receive_pdu , OTHER_SERVER , ev
209231 )
210232 self .get_success (d )
233+ # Play nice and finish the logcontext
234+ with PreserveLoggingContext ():
235+ with send_rejected_logcontext :
236+ pass
211237
212238 # that should have been rejected
213239 e = self .get_success (self .store .get_event (ev .event_id , allow_rejected = True ))
@@ -323,7 +349,14 @@ def test_backfill_with_many_backward_extremities(self) -> None:
323349
324350 current_depth = 1
325351 limit = 100
326- with LoggingContext ("receive_pdu" ):
352+ # To avoid "metrics will be lost" errors from running this function in the
353+ # sentinel logcontext, let's setup a logcontext to use. We use
354+ # `PreserveLoggingContext` so that the logcontext doesn't finish to avoid
355+ # "Re-starting finished log context" errors as we pump in `get_success(...)`. We
356+ # use `run_in_background` so that we run the task in the given logcontext and
357+ # handle the magic behind the scenes.
358+ receive_pdu_logcontext = LoggingContext ("receive_pdu" )
359+ with PreserveLoggingContext (receive_pdu_logcontext ):
327360 # Make sure backfill still works
328361 d = run_in_background (
329362 self .hs .get_federation_handler ().maybe_backfill ,
@@ -332,6 +365,10 @@ def test_backfill_with_many_backward_extremities(self) -> None:
332365 limit ,
333366 )
334367 self .get_success (d )
368+ # Play nice and finish the logcontext
369+ with PreserveLoggingContext ():
370+ with receive_pdu_logcontext :
371+ pass
335372
336373 def test_backfill_ignores_known_events (self ) -> None :
337374 """
@@ -485,19 +522,37 @@ def create_invite() -> EventBase:
485522 def _build_and_send_join_event (
486523 self , other_server : str , other_user : str , room_id : str
487524 ) -> EventBase :
488- join_event = self .get_success (
489- self .handler .on_make_join_request (other_server , room_id , other_user )
490- )
525+ # To avoid "metrics will be lost" errors from running this function in the
526+ # sentinel logcontext, let's setup a logcontext to use. We use
527+ # `PreserveLoggingContext` so that the logcontext doesn't finish to avoid
528+ # "Re-starting finished log context" errors as we pump in `get_success(...)`. We
529+ # use `run_in_background` so that we run the task in the given logcontext and
530+ # handle the magic behind the scenes.
531+ make_join_logcontext = LoggingContext ("make_join" )
532+ with PreserveLoggingContext (make_join_logcontext ):
533+ d = run_in_background (
534+ self .handler .on_make_join_request , other_server , room_id , other_user
535+ )
536+ join_event = self .get_success (d )
537+ # Play nice and finish the logcontext
538+ with PreserveLoggingContext ():
539+ with make_join_logcontext :
540+ pass
491541 # the auth code requires that a signature exists, but doesn't check that
492542 # signature... go figure.
493543 join_event .signatures [other_server ] = {"x" : "y" }
494- with LoggingContext ("send_join" ):
544+
545+ send_join_logcontext = LoggingContext ("send_join" )
546+ with PreserveLoggingContext (send_join_logcontext ):
495547 d = run_in_background (
496548 self .hs .get_federation_event_handler ().on_send_membership_event ,
497549 other_server ,
498550 join_event ,
499551 )
500552 self .get_success (d )
553+ with PreserveLoggingContext ():
554+ with send_join_logcontext :
555+ pass
501556
502557 # sanity-check: the room should show that the new user is a member
503558 r = self .get_success (self .store .get_partial_current_state_ids (room_id ))
0 commit comments