From 5094cdc9f754f60cbd205193940be08053a5e84e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir?= Date: Thu, 12 Mar 2020 14:27:36 +0000 Subject: [PATCH] [FIX] iap: limit force_create at last step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The modification introduced in 5307f47d975c3b3ebd87c7f7e2248802a1aa5be7 was not properly managing the 'force_create' argument. When it is set to false, we only look for existing accounts with the current cursor and end up returning an empty recordset when the account was created during the same transaction. Ex: // Do stuff IapAcc = self.env['iap.account'] IapAcc.get('my_service') IapAcc.get_credit('my_service') // Other stuff closes odoo/odoo#48201 X-original-commit: c78070dc22f99867b4b1f3a52cd11543da571fe4 Signed-off-by: Rémi Rahir (rar) --- addons/iap/models/iap.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/addons/iap/models/iap.py b/addons/iap/models/iap.py index fbecec856c6..4dfd08b88c0 100644 --- a/addons/iap/models/iap.py +++ b/addons/iap/models/iap.py @@ -171,20 +171,23 @@ class IapAccount(models.Model): ] accounts = self.search(domain, order='id desc') if not accounts: - if force_create: - with self.pool.cursor() as cr: - # Since the account did not exist yet, we will encounter a NoCreditError, - # which is going to rollback the database and undo the account creation, - # preventing the process to continue any further. - IapAccount = self.with_env(self.env(cr=cr)) - account = IapAccount.search(domain, order='id desc', limit=1) - if not account: - account = IapAccount.create({'service_name': service_name}) - # fetch 'account_token' into cache with this cursor, - # as self's cursor cannot see this account - account.account_token - return self.browse(account.id) - return accounts + with self.pool.cursor() as cr: + # Since the account did not exist yet, we will encounter a NoCreditError, + # which is going to rollback the database and undo the account creation, + # preventing the process to continue any further. + + # Flush the pending operations to avoid a deadlock. + self.flush() + IapAccount = self.with_env(self.env(cr=cr)) + account = IapAccount.search(domain, order='id desc', limit=1) + if not account: + if not force_create: + return account + account = IapAccount.create({'service_name': service_name}) + # fetch 'account_token' into cache with this cursor, + # as self's cursor cannot see this account + account.account_token + return self.browse(account.id) accounts_with_company = accounts.filtered(lambda acc: acc.company_ids) if accounts_with_company: return accounts_with_company[0]