From 99c319e03a0f08d87bdff95dc0578672fbe89961 Mon Sep 17 00:00:00 2001 From: Toufik Ben Jaa Date: Fri, 4 Jan 2019 09:45:43 +0000 Subject: [PATCH] [FIX] account: deadlock on install MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - When installing an accounting l10n module, the method `try_loading_for_current_company` is called and could cause a deadlock. The deadlock is caused by the fact that if the installation is started via a web request the code tries to retrieve the current user's company by doing `request.env.user.company_id`. This causes issues since the installation code has its own postgresql cursor while calling `request.env.user.company_id` will create its own cursor. So two cursors exists for the same Odoo worker. The installation cursor asks for a lock on `res_users` while the second cursor (the request one) will try to read on `res_users` and wait the first lock to be dropped. The lock is never dropped since the python code isn't running anymore waiting for the `request.env.user.company_id` to be completed. Causing the deadlock. To fix the issue, we avoid the creation/usage of a cursor created by `requesŧ`. So to retrieve the current user's company, we read the user id on the request's session, then browse the `res.user` record with the installation cusror. Fix issue introduced by commit d40956c7909bffa338fd0138565044571e852da3 closes odoo/odoo#29927 --- addons/account/models/chart_template.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index df1bd50869c..6e4256cb230 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -165,9 +165,10 @@ class AccountChartTemplate(models.Model): of accounts had been created for it yet. """ self.ensure_one() - if request and request.db: - company_id = request.env.user.company_id.id - company = self.env['res.company'].browse(company_id) + # do not use `request.env` here, it can cause deadlocks + if request and request.session.uid: + current_user = self.env['res.users'].browse(request.uid) + company = current_user.company_id else: # fallback to company of current user, most likely __system__ # (won't work well for multi-company) @@ -185,9 +186,10 @@ class AccountChartTemplate(models.Model): rights. """ self.ensure_one() - if request and request.db: - company_id = request.env.user.company_id.id - company = self.env['res.company'].browse(company_id) + # do not use `request.env` here, it can cause deadlocks + if request and request.session.uid: + current_user = self.env['res.users'].browse(request.uid) + company = current_user.company_id else: # fallback to company of current user, most likely __system__ # (won't work well for multi-company)