Skip to content

Commit 067b00d

Browse files
committed
Run the reactor with the sentinel logcontext
This fixes a class of 'Unexpected logcontext' messages, which were happening because the logcontext was somewhat arbitrarily swapping between the sentinel and the `run` logcontext.
1 parent 994d7ae commit 067b00d

8 files changed

Lines changed: 51 additions & 16 deletions

File tree

synapse/app/appservice.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from synapse.storage.engines import create_engine
3030
from synapse.util.async import sleep
3131
from synapse.util.httpresourcetree import create_resource_tree
32-
from synapse.util.logcontext import LoggingContext
32+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
3333
from synapse.util.manhole import manhole
3434
from synapse.util.rlimit import change_resource_limit
3535
from synapse.util.versionstring import get_version_string
@@ -187,7 +187,11 @@ def start(config_options):
187187
ps.start_listening(config.worker_listeners)
188188

189189
def run():
190-
with LoggingContext("run"):
190+
# make sure that we run the reactor with the sentinel log context,
191+
# otherwise other PreserveLoggingContext instances will get confused
192+
# and complain when they see the logcontext arbitrarily swapping
193+
# between the sentinel and `run` logcontexts.
194+
with PreserveLoggingContext():
191195
logger.info("Running")
192196
change_resource_limit(config.soft_file_limit)
193197
if config.gc_thresholds:

synapse/app/client_reader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from synapse.storage.engines import create_engine
3636
from synapse.util.async import sleep
3737
from synapse.util.httpresourcetree import create_resource_tree
38-
from synapse.util.logcontext import LoggingContext
38+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
3939
from synapse.util.manhole import manhole
4040
from synapse.util.rlimit import change_resource_limit
4141
from synapse.util.versionstring import get_version_string
@@ -193,7 +193,11 @@ def start(config_options):
193193
ss.start_listening(config.worker_listeners)
194194

195195
def run():
196-
with LoggingContext("run"):
196+
# make sure that we run the reactor with the sentinel log context,
197+
# otherwise other PreserveLoggingContext instances will get confused
198+
# and complain when they see the logcontext arbitrarily swapping
199+
# between the sentinel and `run` logcontexts.
200+
with PreserveLoggingContext():
197201
logger.info("Running")
198202
change_resource_limit(config.soft_file_limit)
199203
if config.gc_thresholds:

synapse/app/federation_reader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from synapse.storage.engines import create_engine
3232
from synapse.util.async import sleep
3333
from synapse.util.httpresourcetree import create_resource_tree
34-
from synapse.util.logcontext import LoggingContext
34+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
3535
from synapse.util.manhole import manhole
3636
from synapse.util.rlimit import change_resource_limit
3737
from synapse.util.versionstring import get_version_string
@@ -184,7 +184,11 @@ def start(config_options):
184184
ss.start_listening(config.worker_listeners)
185185

186186
def run():
187-
with LoggingContext("run"):
187+
# make sure that we run the reactor with the sentinel log context,
188+
# otherwise other PreserveLoggingContext instances will get confused
189+
# and complain when they see the logcontext arbitrarily swapping
190+
# between the sentinel and `run` logcontexts.
191+
with PreserveLoggingContext():
188192
logger.info("Running")
189193
change_resource_limit(config.soft_file_limit)
190194
if config.gc_thresholds:

synapse/app/federation_sender.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from synapse.storage.presence import UserPresenceState
3636
from synapse.util.async import sleep
3737
from synapse.util.httpresourcetree import create_resource_tree
38-
from synapse.util.logcontext import LoggingContext
38+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
3939
from synapse.util.manhole import manhole
4040
from synapse.util.rlimit import change_resource_limit
4141
from synapse.util.versionstring import get_version_string
@@ -193,7 +193,11 @@ def start(config_options):
193193
ps.start_listening(config.worker_listeners)
194194

195195
def run():
196-
with LoggingContext("run"):
196+
# make sure that we run the reactor with the sentinel log context,
197+
# otherwise other PreserveLoggingContext instances will get confused
198+
# and complain when they see the logcontext arbitrarily swapping
199+
# between the sentinel and `run` logcontexts.
200+
with PreserveLoggingContext():
197201
logger.info("Running")
198202
change_resource_limit(config.soft_file_limit)
199203
if config.gc_thresholds:

synapse/app/homeserver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
)
5353
from synapse.config.homeserver import HomeServerConfig
5454
from synapse.crypto import context_factory
55-
from synapse.util.logcontext import LoggingContext
55+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
5656
from synapse.metrics import register_memory_metrics, get_metrics_for
5757
from synapse.metrics.resource import MetricsResource, METRICS_PREFIX
5858
from synapse.replication.resource import ReplicationResource, REPLICATION_PREFIX
@@ -456,7 +456,12 @@ def phone_stats_home():
456456
def in_thread():
457457
# Uncomment to enable tracing of log context changes.
458458
# sys.settrace(logcontext_tracer)
459-
with LoggingContext("run"):
459+
460+
# make sure that we run the reactor with the sentinel log context,
461+
# otherwise other PreserveLoggingContext instances will get confused
462+
# and complain when they see the logcontext arbitrarily swapping
463+
# between the sentinel and `run` logcontexts.
464+
with PreserveLoggingContext():
460465
change_resource_limit(hs.config.soft_file_limit)
461466
if hs.config.gc_thresholds:
462467
gc.set_threshold(*hs.config.gc_thresholds)

synapse/app/media_repository.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from synapse.storage.media_repository import MediaRepositoryStore
3333
from synapse.util.async import sleep
3434
from synapse.util.httpresourcetree import create_resource_tree
35-
from synapse.util.logcontext import LoggingContext
35+
from synapse.util.logcontext import LoggingContext, PreserveLoggingContext
3636
from synapse.util.manhole import manhole
3737
from synapse.util.rlimit import change_resource_limit
3838
from synapse.util.versionstring import get_version_string
@@ -190,7 +190,11 @@ def start(config_options):
190190
ss.start_listening(config.worker_listeners)
191191

192192
def run():
193-
with LoggingContext("run"):
193+
# make sure that we run the reactor with the sentinel log context,
194+
# otherwise other PreserveLoggingContext instances will get confused
195+
# and complain when they see the logcontext arbitrarily swapping
196+
# between the sentinel and `run` logcontexts.
197+
with PreserveLoggingContext():
194198
logger.info("Running")
195199
change_resource_limit(config.soft_file_limit)
196200
if config.gc_thresholds:

synapse/app/pusher.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from synapse.storage import DataStore
3232
from synapse.util.async import sleep
3333
from synapse.util.httpresourcetree import create_resource_tree
34-
from synapse.util.logcontext import LoggingContext, preserve_fn
34+
from synapse.util.logcontext import LoggingContext, preserve_fn, \
35+
PreserveLoggingContext
3536
from synapse.util.manhole import manhole
3637
from synapse.util.rlimit import change_resource_limit
3738
from synapse.util.versionstring import get_version_string
@@ -275,7 +276,11 @@ def start(config_options):
275276
ps.start_listening(config.worker_listeners)
276277

277278
def run():
278-
with LoggingContext("run"):
279+
# make sure that we run the reactor with the sentinel log context,
280+
# otherwise other PreserveLoggingContext instances will get confused
281+
# and complain when they see the logcontext arbitrarily swapping
282+
# between the sentinel and `run` logcontexts.
283+
with PreserveLoggingContext():
279284
logger.info("Running")
280285
change_resource_limit(config.soft_file_limit)
281286
if config.gc_thresholds:

synapse/app/synchrotron.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
from synapse.storage.roommember import RoomMemberStore
4949
from synapse.util.async import sleep
5050
from synapse.util.httpresourcetree import create_resource_tree
51-
from synapse.util.logcontext import LoggingContext, preserve_fn
51+
from synapse.util.logcontext import LoggingContext, preserve_fn, \
52+
PreserveLoggingContext
5253
from synapse.util.manhole import manhole
5354
from synapse.util.rlimit import change_resource_limit
5455
from synapse.util.stringutils import random_string
@@ -496,7 +497,11 @@ def start(config_options):
496497
ss.start_listening(config.worker_listeners)
497498

498499
def run():
499-
with LoggingContext("run"):
500+
# make sure that we run the reactor with the sentinel log context,
501+
# otherwise other PreserveLoggingContext instances will get confused
502+
# and complain when they see the logcontext arbitrarily swapping
503+
# between the sentinel and `run` logcontexts.
504+
with PreserveLoggingContext():
500505
logger.info("Running")
501506
change_resource_limit(config.soft_file_limit)
502507
if config.gc_thresholds:

0 commit comments

Comments
 (0)