From f03b0140d535aa9149b12f1c76d0c8196f0f79ab Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Thu, 9 Feb 2023 09:29:38 +0100 Subject: [PATCH] [FIX] runbot: warn when a local pg backend cannot be terminated For whatever reason, it happens that we open a testing database in a psql console as another user. In that case, the runbot is unable to terminate the backend and goes in an error loop. --- runbot/models/build.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runbot/models/build.py b/runbot/models/build.py index a2355ffa..475f356c 100644 --- a/runbot/models/build.py +++ b/runbot/models/build.py @@ -19,6 +19,7 @@ from odoo.tools.safe_eval import safe_eval from collections import defaultdict from pathlib import Path from psycopg2 import sql +from psycopg2.errors import InsufficientPrivilege import getpass _logger = logging.getLogger(__name__) @@ -876,7 +877,11 @@ class BuildResult(models.Model): def _local_pg_dropdb(self, dbname): with local_pgadmin_cursor() as local_cr: pid_col = 'pid' if local_cr.connection.server_version >= 90200 else 'procpid' - query = 'SELECT pg_terminate_backend({}) FROM pg_stat_activity WHERE datname=%s'.format(pid_col) + try: + query = 'SELECT pg_terminate_backend({}) FROM pg_stat_activity WHERE datname=%s'.format(pid_col) + except InsufficientPrivilege: + _logger.warning('Cannot terminate backend process for %s (maybe a root psql console is accessing the database).', dbname) + return local_cr.execute(query, [dbname]) local_cr.execute('DROP DATABASE IF EXISTS "%s"' % dbname) # cleanup filestore