From 861d13c513ab99b027ea94f192a0438c1f6ee542 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Tue, 29 Nov 2022 09:36:52 -0800 Subject: [PATCH] DB locks shouldn't squelch errors Change-Id: Icca4718a1cea1d21c1e858e9048552946a9f739a --- swift/common/db.py | 19 +++++++++---------- test/unit/common/test_db.py | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/swift/common/db.py b/swift/common/db.py index 3e42d952ae..d211031912 100644 --- a/swift/common/db.py +++ b/swift/common/db.py @@ -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()) diff --git a/test/unit/common/test_db.py b/test/unit/common/test_db.py index 8a3e11a804..75de2488bb 100644 --- a/test/unit/common/test_db.py +++ b/test/unit/common/test_db.py @@ -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)