Skip to content

Commit 7a9725c

Browse files
committed
WIP on clean shutdown of SynapseHomeServer class
1 parent 8fb9c10 commit 7a9725c

121 files changed

Lines changed: 498 additions & 333 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

synapse/api/auth/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020
#
2121
import logging
22+
import weakref
2223
from typing import TYPE_CHECKING, Optional, Tuple
2324

2425
from netaddr import IPAddress
@@ -52,7 +53,7 @@ class BaseAuth:
5253
"""Common base class for all auth implementations."""
5354

5455
def __init__(self, hs: "HomeServer"):
55-
self.hs = hs
56+
self.hs = weakref.proxy(hs)
5657
self.store = hs.get_datastores().main
5758
self._storage_controllers = hs.get_storage_controllers()
5859

synapse/api/auth_blocking.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121

2222
import logging
23+
import weakref
2324
from typing import TYPE_CHECKING, Optional
2425

2526
from synapse.api.constants import LimitBlockingTypes, UserTypes
@@ -46,7 +47,7 @@ def __init__(self, hs: "HomeServer"):
4647
self._mau_limits_reserved_threepids = (
4748
hs.config.server.mau_limits_reserved_threepids
4849
)
49-
self._is_mine_server_name = hs.is_mine_server_name
50+
self.hs = weakref.proxy(hs)
5051
self._track_appservice_user_ips = hs.config.appservice.track_appservice_user_ips
5152

5253
async def check_auth_blocking(
@@ -84,7 +85,7 @@ async def check_auth_blocking(
8485
if requester:
8586
if requester.authenticated_entity.startswith("@"):
8687
user_id = requester.authenticated_entity
87-
elif self._is_mine_server_name(requester.authenticated_entity):
88+
elif self.hs._is_mine_server_name(requester.authenticated_entity):
8889
# We never block the server from doing actions on behalf of
8990
# users.
9091
return

synapse/api/filtering.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#
2222
#
2323
import json
24+
import weakref
2425
from typing import (
2526
TYPE_CHECKING,
2627
Awaitable,
@@ -152,7 +153,7 @@ def matrix_user_id_validator(user_id: object) -> bool:
152153

153154
class Filtering:
154155
def __init__(self, hs: "HomeServer"):
155-
self._hs = hs
156+
self._hs = weakref.proxy(hs)
156157
self.store = hs.get_datastores().main
157158

158159
self.DEFAULT_FILTER_COLLECTION = FilterCollection(hs, {})
@@ -320,7 +321,7 @@ def blocks_all_room_timeline(self) -> bool:
320321

321322
class Filter:
322323
def __init__(self, hs: "HomeServer", filter_json: JsonMapping):
323-
self._hs = hs
324+
self._hs = weakref.proxy(hs)
324325
self._store = hs.get_datastores().main
325326
self.filter_json = filter_json
326327

synapse/app/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Tuple,
4141
cast,
4242
)
43+
import weakref
4344

4445
from cryptography.utils import CryptographyDeprecationWarning
4546
from typing_extensions import ParamSpec
@@ -551,7 +552,7 @@ def run_sighup(*args: Any, **kwargs: Any) -> None:
551552

552553
signal.signal(signal.SIGHUP, run_sighup)
553554

554-
register_sighup(refresh_certificate, hs)
555+
register_sighup(refresh_certificate, weakref.proxy(hs))
555556
register_sighup(reload_cache_config, hs.config)
556557

557558
# Apply the cache config.

synapse/app/phone_stats_home.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ def performance_stats_init() -> None:
198198
# Rather than update on per session basis, batch up the requests.
199199
# If you increase the loop period, the accuracy of user_daily_visits
200200
# table will decrease
201-
clock.looping_call(
202-
hs.get_datastores().main.generate_user_daily_visits,
203-
5 * ONE_MINUTE_SECONDS * MILLISECONDS_PER_SECOND,
201+
hs.register_looping_call(
202+
clock.looping_call(
203+
hs.get_datastores().main.generate_user_daily_visits,
204+
5 * MILLISECONDS_PER_SECOND,
205+
)
204206
)
205207

206208
# monthly active user limiting functionality
@@ -237,11 +239,13 @@ async def generate_monthly_active_users() -> None:
237239

238240
if hs.config.metrics.report_stats:
239241
logger.info("Scheduling stats reporting for 3 hour intervals")
240-
clock.looping_call(
241-
phone_stats_home,
242-
PHONE_HOME_INTERVAL_SECONDS * MILLISECONDS_PER_SECOND,
243-
hs,
244-
stats,
242+
hs.register_looping_call(
243+
clock.looping_call(
244+
phone_stats_home,
245+
PHONE_HOME_INTERVAL_SECONDS * MILLISECONDS_PER_SECOND,
246+
hs,
247+
stats,
248+
)
245249
)
246250

247251
# We need to defer this init for the cases that we daemonize

synapse/crypto/keyring.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import abc
2323
import logging
24+
import weakref
2425
from typing import TYPE_CHECKING, Callable, Dict, Iterable, List, Optional, Tuple
2526

2627
import attr
@@ -175,7 +176,7 @@ def __init__(
175176
process_batch_callback=self._inner_fetch_key_requests,
176177
)
177178

178-
self._is_mine_server_name = hs.is_mine_server_name
179+
self.hs = weakref.proxy(hs)
179180

180181
# build a FetchKeyResult for each of our own keys, to shortcircuit the
181182
# fetcher.
@@ -279,7 +280,7 @@ async def process_request(self, verify_request: VerifyJsonRequest) -> None:
279280

280281
# If we are the originating server, short-circuit the key-fetch for any keys
281282
# we already have
282-
if self._is_mine_server_name(verify_request.server_name):
283+
if self.hs._is_mine_server_name(verify_request.server_name):
283284
for key_id in verify_request.key_ids:
284285
if key_id in self._local_verify_keys:
285286
found_keys[key_id] = self._local_verify_keys[key_id]

synapse/federation/federation_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121
#
2222
import logging
23+
import weakref
2324
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional, Sequence
2425

2526
from synapse.api.constants import MAX_DEPTH, EventContentFields, EventTypes, Membership
@@ -56,9 +57,8 @@ def __init__(self, message: str, event_id: str):
5657

5758
class FederationBase:
5859
def __init__(self, hs: "HomeServer"):
59-
self.hs = hs
60+
self.hs = weakref.proxy(hs)
6061

61-
self._is_mine_server_name = hs.is_mine_server_name
6262
self.keyring = hs.get_keyring()
6363
self._spam_checker_module_callbacks = hs.get_module_api_callbacks().spam_checker
6464
self.store = hs.get_datastores().main

synapse/federation/federation_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ async def _try_destination_list(
926926

927927
for destination in destinations:
928928
# We don't want to ask our own server for information we don't have
929-
if self._is_mine_server_name(destination):
929+
if self.hs._is_mine_server_name(destination):
930930
continue
931931

932932
try:
@@ -1606,7 +1606,7 @@ async def forward_third_party_invite(
16061606
self, destinations: Iterable[str], room_id: str, event_dict: JsonDict
16071607
) -> None:
16081608
for destination in destinations:
1609-
if self._is_mine_server_name(destination):
1609+
if self.hs._is_mine_server_name(destination):
16101610
continue
16111611

16121612
try:

synapse/federation/federation_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ async def _on_send_membership_event(
980980
authorising_server = get_domain_from_id(
981981
event.content[EventContentFields.AUTHORISING_USER]
982982
)
983-
if not self._is_mine_server_name(authorising_server):
983+
if not self.hs._is_mine_server_name(authorising_server):
984984
raise SynapseError(
985985
400,
986986
f"Cannot authorise membership event for {authorising_server}. We can only authorise requests from our own homeserver",

synapse/federation/send_queue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"""
3838

3939
import logging
40+
import weakref
4041
from typing import (
4142
TYPE_CHECKING,
4243
Dict,
@@ -74,8 +75,7 @@ def __init__(self, hs: "HomeServer"):
7475
self.server_name = hs.hostname
7576
self.clock = hs.get_clock()
7677
self.notifier = hs.get_notifier()
77-
self.is_mine_id = hs.is_mine_id
78-
self.is_mine_server_name = hs.is_mine_server_name
78+
self.hs = weakref.proxy(hs)
7979

8080
# We may have multiple federation sender instances, so we need to track
8181
# their positions separately.
@@ -208,7 +208,7 @@ def build_and_send_edu(
208208
key: Optional[Hashable] = None,
209209
) -> None:
210210
"""As per FederationSender"""
211-
if self.is_mine_server_name(destination):
211+
if self.hs.is_mine_server_name(destination):
212212
logger.info("Not sending EDU to ourselves")
213213
return
214214

0 commit comments

Comments
 (0)