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
         )
34
         )
35
         self._current_workspace = None  # type: Workspace
35
         self._current_workspace = None  # type: Workspace
36
         self._current_user = None  # type: User
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
     @property
40
     @property
39
     def current_workspace(self) -> Workspace:
41
     def current_workspace(self) -> Workspace:
74
             )
76
             )
75
         self._current_user = user
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
 # Utils for TracimRequest
93
 # Utils for TracimRequest

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

266
         self.app_config.configure_filedepot()
266
         self.app_config.configure_filedepot()
267
 
267
 
268
     def __call__(self, environ, start_response):
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
 class TracimUserSession(BaseMiddleware):
282
 class TracimUserSession(BaseMiddleware):
288
             session=environ['tracim_dbsession'],
292
             session=environ['tracim_dbsession'],
289
             config=environ['tracim_cfg'],
293
             config=environ['tracim_cfg'],
290
         ).get_one_by_email(environ['http_authenticator.username'])
294
         ).get_one_by_email(environ['http_authenticator.username'])
291
-        return self._application(environ, start_response)
295
+        return self._application(environ, start_response)