summary refs log tree commit diff
path: root/synapse/server.py
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2018-08-28 13:39:49 +0100
committerRichard van der Hoff <richard@matrix.org>2018-08-28 13:39:49 +0100
commit0b07f02e19e5ed1eba0e74c3863def9f849b5f9a (patch)
treedd17eca36b5b169dc25b309631b4522b3d516cc7 /synapse/server.py
parentMerge pull request #3758 from matrix-org/erikj/admin_contact (diff)
downloadsynapse-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.py15
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):