Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit eddce9d

Browse files
authored
Merge pull request #2027 from matrix-org/rav/logcontext_leaks
A few fixes to logcontext things
2 parents 2e05f5d + f40c2db commit eddce9d

12 files changed

Lines changed: 147 additions & 62 deletions

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:

synapse/handlers/federation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,9 @@ def do_invite_join(self, target_hosts, room_id, joinee, content):
933933
# lots of requests for missing prev_events which we do actually
934934
# have. Hence we fire off the deferred, but don't wait for it.
935935

936-
synapse.util.logcontext.reset_context_after_deferred(
937-
self._handle_queued_pdus(room_queue))
936+
synapse.util.logcontext.preserve_fn(self._handle_queued_pdus)(
937+
room_queue
938+
)
938939

939940
defer.returnValue(True)
940941

synapse/storage/background_updates.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import synapse.util.async
1516

1617
from ._base import SQLBaseStore
1718
from . import engines
@@ -84,24 +85,14 @@ def __init__(self, hs):
8485
self._background_update_performance = {}
8586
self._background_update_queue = []
8687
self._background_update_handlers = {}
87-
self._background_update_timer = None
8888

8989
@defer.inlineCallbacks
9090
def start_doing_background_updates(self):
91-
assert self._background_update_timer is None, \
92-
"background updates already running"
93-
9491
logger.info("Starting background schema updates")
9592

9693
while True:
97-
sleep = defer.Deferred()
98-
self._background_update_timer = self._clock.call_later(
99-
self.BACKGROUND_UPDATE_INTERVAL_MS / 1000., sleep.callback, None
100-
)
101-
try:
102-
yield sleep
103-
finally:
104-
self._background_update_timer = None
94+
yield synapse.util.async.sleep(
95+
self.BACKGROUND_UPDATE_INTERVAL_MS / 1000.)
10596

10697
try:
10798
result = yield self.do_next_background_update(

0 commit comments

Comments
 (0)