From 507295ca80e8eac9c75714f41b599d9e2e8a2549 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 6 Oct 2020 09:12:05 +0000 Subject: [FIX] core: preferably flush() with an env with a real uid This fixes a crash in some controllers with `auth='None'` where some updates are flushed with an environment where `uid=None`. When there is an environment with a real uid, preferably use it. closes odoo/odoo#59650 X-original-commit: 2795c86df54389b858a454cbc1782b1f34f3ba4f Signed-off-by: Raphael Collet (rco) --- odoo/sql_db.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/odoo/sql_db.py b/odoo/sql_db.py index ce49fb6a60a..1c15f4e4a2d 100644 --- a/odoo/sql_db.py +++ b/odoo/sql_db.py @@ -66,13 +66,18 @@ def flush_env(cr, *, clear=True): """ Retrieve and flush an environment corresponding to the given cursor. Also clear the environment if ``clear`` is true. """ + env_to_flush = None for env in list(Environment.envs): # don't flush() on another cursor or with a RequestUID if env.cr is cr and (isinstance(env.uid, int) or env.uid is None): - env['base'].flush() - if clear: - env.clear() # clear remaining new records to compute - break + env_to_flush = env + if env.uid is not None: + break # prefer an environment with a real uid + + if env_to_flush is not None: + env_to_flush['base'].flush() + if clear: + env_to_flush.clear() # clear remaining new records to compute def clear_env(cr): """ Retrieve and clear an environment corresponding to the given cursor """ -- cgit v1.2.3