Browse Source

Merge branch 'develop' of github.com:tracim/tracim_backend into fix/few_more_tests

Guénaël Muller 6 years ago
parent
commit
13d06d13fd
2 changed files with 26 additions and 8 deletions
  1. 14 0
      tracim/lib/utils/request.py
  2. 12 8
      tracim/lib/webdav/middlewares.py

+ 14 - 0
tracim/lib/utils/request.py View File

@@ -34,6 +34,8 @@ class TracimRequest(Request):
34 34
         )
35 35
         self._current_workspace = None  # type: Workspace
36 36
         self._current_user = None  # type: User
37
+        # INFO - G.M - 18-05-2018 - Close db at the end of the request
38
+        self.add_finished_callback(self._cleanup)
37 39
 
38 40
     @property
39 41
     def current_workspace(self) -> Workspace:
@@ -74,6 +76,18 @@ class TracimRequest(Request):
74 76
             )
75 77
         self._current_user = user
76 78
 
79
+    def _cleanup(self, request: 'TracimRequest') -> None:
80
+        """
81
+        Close dbsession at the end of the request in order to avoid exception
82
+        about not properly closed session or "object created in another thread"
83
+        issue
84
+        see https://github.com/tracim/tracim_backend/issues/62
85
+        :param request: same as self, request
86
+        :return: nothing.
87
+        """
88
+        self._current_user = None
89
+        self._current_workspace = None
90
+        self.dbsession.close()
77 91
 
78 92
 ###
79 93
 # Utils for TracimRequest

+ 12 - 8
tracim/lib/webdav/middlewares.py View File

@@ -266,13 +266,17 @@ class TracimEnv(BaseMiddleware):
266 266
         self.app_config.configure_filedepot()
267 267
 
268 268
     def __call__(self, environ, start_response):
269
-        with transaction.manager as tm:
270
-            dbsession = get_tm_session(self.session_factory, transaction.manager)
271
-            environ['tracim_tm'] = tm
272
-            environ['tracim_dbsession'] = dbsession
273
-            environ['tracim_cfg'] = self.app_config
274
-
275
-            return self._application(environ, start_response)
269
+        # TODO - G.M - 18-05-2018 - This code should not create trouble
270
+        # with thread and database, this should be verify.
271
+        # see https://github.com/tracim/tracim_backend/issues/62
272
+        tm = transaction.manager
273
+        dbsession = get_tm_session(self.session_factory, tm)
274
+        environ['tracim_tm'] = tm
275
+        environ['tracim_dbsession'] = dbsession
276
+        environ['tracim_cfg'] = self.app_config
277
+        app = self._application(environ, start_response)
278
+        dbsession.close()
279
+        return app
276 280
 
277 281
 
278 282
 class TracimUserSession(BaseMiddleware):
@@ -288,4 +292,4 @@ class TracimUserSession(BaseMiddleware):
288 292
             session=environ['tracim_dbsession'],
289 293
             config=environ['tracim_cfg'],
290 294
         ).get_one_by_email(environ['http_authenticator.username'])
291
-        return self._application(environ, start_response)
295
+        return self._application(environ, start_response)