Merge "DB locks shouldn't squelch errors"

This commit is contained in:
Zuul 2022-12-02 16:13:35 +00:00 committed by Gerrit Code Review
commit d503ddaf14
2 changed files with 26 additions and 13 deletions

View File

@ -581,16 +581,15 @@ class DatabaseBroker(object):
conn.execute('BEGIN IMMEDIATE')
try:
yield True
except (Exception, Timeout):
pass
try:
conn.execute('ROLLBACK')
conn.isolation_level = orig_isolation_level
self.conn = conn
except (Exception, Timeout):
logging.exception(
_('Broker error trying to rollback locked connection'))
conn.close()
finally:
try:
conn.execute('ROLLBACK')
conn.isolation_level = orig_isolation_level
self.conn = conn
except (Exception, Timeout):
logging.exception(
_('Broker error trying to rollback locked connection'))
conn.close()
def _new_db_id(self):
device_name = os.path.basename(self.get_device_path())

View File

@ -914,13 +914,27 @@ class TestDatabaseBroker(unittest.TestCase):
pass
with broker.lock():
pass
with self.assertRaises(RuntimeError) as raised, broker.lock():
raise RuntimeError('boom!')
self.assertEqual(raised.exception.args[0], 'boom!')
broker2 = DatabaseBroker(os.path.join(self.testdir, '1.db'),
timeout=.1)
broker2._initialize = stub
with broker.lock():
with self.assertRaises(LockTimeout) as raised, \
broker2.lock():
pass
# broker2 raises the timeout
with self.assertRaises(LockTimeout) as raised:
with broker2.lock():
pass
self.assertEqual(str(raised.exception),
'0.1 seconds: %s' % broker.db_file)
# and the timeout bubbles up out of broker.lock()
with self.assertRaises(LockTimeout) as raised:
with broker.lock():
with broker2.lock():
pass
self.assertEqual(str(raised.exception),
'0.1 seconds: %s' % broker.db_file)