From 2b27e1bd8015a26694ba66e95f5d62f19508b6ed Mon Sep 17 00:00:00 2001 From: joda-odoo Date: Tue, 23 Apr 2024 09:13:41 +0200 Subject: [PATCH] [IMP] base: restrict _gc_sessions to a single database Background ========= Previously, _gc_sessions ran for all databases regardless of the active one. This could cause problems in SaaS environments with shared session storage across databases. Change ====== We propose introducing a limitation within the _gc_sessions method. The method will now check for the presence of the ODOO_SKIP_GC_SESSIONS environment variable. If set, it skips the cleanup process. This behavior is typically desired in environments launched without a specified database. On the other hand, if the environment variable is not set, the method will likely assume a non-SaaS environment (e.g., on-premise or development) and continue with the regular cleanup process. Impact ===== Ensures isolated session management for individual databases in SaaS environments. Maintains automatic cleanup for developers and on-premise users. task-3716956 closes odoo/odoo#152880 Signed-off-by: Julien Castiaux (juc) --- odoo/addons/base/models/ir_http.py | 2 ++ odoo/addons/test_http/tests/test_session.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/odoo/addons/base/models/ir_http.py b/odoo/addons/base/models/ir_http.py index 2485e52be4743..006e10c9b08c7 100644 --- a/odoo/addons/base/models/ir_http.py +++ b/odoo/addons/base/models/ir_http.py @@ -270,6 +270,8 @@ def routing_map(self, key=None): @api.autovacuum def _gc_sessions(self): + if os.getenv("ODOO_SKIP_GC_SESSIONS"): + return http.root.session_store.vacuum(max_lifetime=http.get_session_max_inactivity(self.env)) @api.model diff --git a/odoo/addons/test_http/tests/test_session.py b/odoo/addons/test_http/tests/test_session.py index ef8786012cb68..d8d5a879a5c5a 100644 --- a/odoo/addons/test_http/tests/test_session.py +++ b/odoo/addons/test_http/tests/test_session.py @@ -1,5 +1,6 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. +import os import datetime import json import pytz @@ -211,6 +212,17 @@ def check_session_attr(value): for value in not_recommended_values: self.assertEqual(check_session_attr(value), None) + @patch("odoo.http.root.session_store.vacuum") + def test_session8_gc_ignored_no_db_name(self, mock): + with patch.dict(os.environ, {'ODOO_SKIP_GC_SESSIONS': ''}): + self.env['ir.http']._gc_sessions() + mock.assert_called_once() + + with patch.dict(os.environ, {'ODOO_SKIP_GC_SESSIONS': '1'}): + mock.reset_mock() + self.env['ir.http']._gc_sessions() + mock.assert_not_called() + class TestSessionStore(HttpCaseWithUserDemo): def setUp(self): super().setUp()