diff options
author | Richard van der Hoff <richard@matrix.org> | 2018-08-28 13:39:49 +0100 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2018-08-28 13:39:49 +0100 |
commit | 0b07f02e19e5ed1eba0e74c3863def9f849b5f9a (patch) | |
tree | dd17eca36b5b169dc25b309631b4522b3d516cc7 /synapse/server.py | |
parent | Merge pull request #3758 from matrix-org/erikj/admin_contact (diff) | |
download | synapse-0b07f02e19e5ed1eba0e74c3863def9f849b5f9a.tar.xz |
Make sure that we close db connections opened during init
We should explicitly close any db connections we open, because failing to do so can block other transactions as per https://github.com/matrix-org/synapse/issues/3682. Let's also try to factor out some of the boilerplate by having server classes define their datastore class rather than duplicating the whole of `setup`.
Diffstat (limited to 'synapse/server.py')
-rw-r--r-- | synapse/server.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/synapse/server.py b/synapse/server.py index a6fbc6ec0c..802d679cde 100644 --- a/synapse/server.py +++ b/synapse/server.py @@ -81,7 +81,6 @@ from synapse.server_notices.server_notices_manager import ServerNoticesManager from synapse.server_notices.server_notices_sender import ServerNoticesSender from synapse.server_notices.worker_server_notices_sender import WorkerServerNoticesSender from synapse.state import StateHandler, StateResolutionHandler -from synapse.storage import DataStore from synapse.streams.events import EventSources from synapse.util import Clock from synapse.util.distributor import Distributor @@ -172,6 +171,11 @@ class HomeServer(object): 'room_context_handler', ] + # This is overridden in derived application classes + # (such as synapse.app.homeserver.SynapseHomeServer) and gives the class to be + # instantiated during setup() for future return by get_datastore() + DATASTORE_CLASS = None + def __init__(self, hostname, reactor=None, **kwargs): """ Args: @@ -188,13 +192,20 @@ class HomeServer(object): self.distributor = Distributor() self.ratelimiter = Ratelimiter() + self.datastore = None + # Other kwargs are explicit dependencies for depname in kwargs: setattr(self, depname, kwargs[depname]) def setup(self): logger.info("Setting up.") - self.datastore = DataStore(self.get_db_conn(), self) + if self.DATASTORE_CLASS is None: + raise RuntimeError("%s does not define a DATASTORE_CLASS" % ( + self.__class__.__name__, + )) + with self.get_db_conn() as conn: + self.datastore = self.DATASTORE_CLASS(conn, self) logger.info("Finished setting up.") def get_reactor(self): |