Browse Source

Merge branch 'develop' of github.com:tracim/tracim_backend into feature/578_add_api_for_workspace/apps

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

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

@@ -42,6 +42,8 @@ class TracimRequest(Request):
42 42
         # User found from request headers, content, distinct from authenticated
43 43
         # user
44 44
         self._user_candidate = None  # type: User
45
+        # INFO - G.M - 18-05-2018 - Close db at the end of the request
46
+        self.add_finished_callback(self._cleanup)
45 47
 
46 48
     @property
47 49
     def current_workspace(self) -> Workspace:
@@ -98,6 +100,20 @@ class TracimRequest(Request):
98 100
             self.candidate_user = get_candidate_user(self)
99 101
         return self._user_candidate
100 102
 
103
+    def _cleanup(self, request: 'TracimRequest') -> None:
104
+        """
105
+        Close dbsession at the end of the request in order to avoid exception
106
+        about not properly closed session or "object created in another thread"
107
+        issue
108
+        see https://github.com/tracim/tracim_backend/issues/62
109
+        :param request: same as self, request
110
+        :return: nothing.
111
+        """
112
+        self._current_user = None
113
+        self._current_workspace = None
114
+        self.dbsession.close()
115
+
116
+
101 117
     @candidate_user.setter
102 118
     def candidate_user(self, user: User) -> None:
103 119
         if self._user_candidate is not None:

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

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