diff --git a/lib/reboot_cmds.py b/lib/reboot_cmds.py index 40e121e340..27b4e1820e 100644 --- a/lib/reboot_cmds.py +++ b/lib/reboot_cmds.py @@ -85,7 +85,7 @@ def main(cfg: config.UAConfig) -> int: LOG.debug("Running reboot commands...") try: - with lock.SpinLock(cfg=cfg, lock_holder="pro-reboot-cmds"): + with lock.RetryLock(cfg=cfg, lock_holder="pro-reboot-cmds"): fix_pro_pkg_holds(cfg) refresh_contract(cfg) upgrade_lts_contract.process_contract_delta_after_apt_lock(cfg) diff --git a/uaclient/api/tests/test_api_u_pro_attach_auto_full_auto_attach_v1.py b/uaclient/api/tests/test_api_u_pro_attach_auto_full_auto_attach_v1.py index 9b41bf3e4d..d2743aeed8 100644 --- a/uaclient/api/tests/test_api_u_pro_attach_auto_full_auto_attach_v1.py +++ b/uaclient/api/tests/test_api_u_pro_attach_auto_full_auto_attach_v1.py @@ -234,7 +234,7 @@ def test_error_full_auto_attach_fail( assert 4 == enable_ent_by_name.call_count @mock.patch( - "uaclient.lock.SpinLock.__enter__", + "uaclient.lock.RetryLock.__enter__", side_effect=[ exceptions.LockHeldError( lock_request="request", lock_holder="holder", pid=10 @@ -242,7 +242,7 @@ def test_error_full_auto_attach_fail( ], ) def test_lock_held( - self, _m_spinlock_enter, _notice_remove, _notice_read, FakeConfig + self, _m_retrylock_enter, _notice_remove, _notice_read, FakeConfig ): with pytest.raises(exceptions.LockHeldError): _full_auto_attach(FullAutoAttachOptions, FakeConfig()) diff --git a/uaclient/api/u/pro/attach/auto/full_auto_attach/v1.py b/uaclient/api/u/pro/attach/auto/full_auto_attach/v1.py index 9875971c62..d126a39c2c 100644 --- a/uaclient/api/u/pro/attach/auto/full_auto_attach/v1.py +++ b/uaclient/api/u/pro/attach/auto/full_auto_attach/v1.py @@ -78,7 +78,7 @@ def _full_auto_attach( mode: event_logger.EventLoggerMode = event_logger.EventLoggerMode.JSON ) -> FullAutoAttachResult: try: - with lock.SpinLock( + with lock.RetryLock( cfg=cfg, lock_holder="pro.api.u.pro.attach.auto.full_auto_attach.v1", ): diff --git a/uaclient/cli/__init__.py b/uaclient/cli/__init__.py index 976f5a5cd1..7c03336a98 100644 --- a/uaclient/cli/__init__.py +++ b/uaclient/cli/__init__.py @@ -180,7 +180,9 @@ def assert_lock_file(lock_holder=None): def wrapper(f): @wraps(f) def new_f(*args, cfg, **kwargs): - with lock.SpinLock(cfg=cfg, lock_holder=lock_holder, sleep_time=1): + with lock.RetryLock( + cfg=cfg, lock_holder=lock_holder, sleep_time=1 + ): retval = f(*args, cfg=cfg, **kwargs) return retval diff --git a/uaclient/daemon/poll_for_pro_license.py b/uaclient/daemon/poll_for_pro_license.py index e6b5d0c347..37e4879139 100644 --- a/uaclient/daemon/poll_for_pro_license.py +++ b/uaclient/daemon/poll_for_pro_license.py @@ -15,7 +15,7 @@ def attempt_auto_attach(cfg: UAConfig, cloud: AutoAttachCloudInstance): try: - with lock.SpinLock( + with lock.RetryLock( cfg=cfg, lock_holder="pro.daemon.attempt_auto_attach" ): actions.auto_attach(cfg, cloud) diff --git a/uaclient/daemon/retry_auto_attach.py b/uaclient/daemon/retry_auto_attach.py index 3960cf9de6..629fb06380 100644 --- a/uaclient/daemon/retry_auto_attach.py +++ b/uaclient/daemon/retry_auto_attach.py @@ -118,7 +118,7 @@ def retry_auto_attach(cfg: UAConfig) -> None: "\n" + auto_attach_status_msg + "\n\n", ) try: - with lock.SpinLock( + with lock.RetryLock( cfg=cfg, lock_holder="pro.daemon.retry_auto_attach.notice_updates", ): diff --git a/uaclient/daemon/tests/test_poll_for_pro_license.py b/uaclient/daemon/tests/test_poll_for_pro_license.py index 2ad2629556..e126d54baf 100644 --- a/uaclient/daemon/tests/test_poll_for_pro_license.py +++ b/uaclient/daemon/tests/test_poll_for_pro_license.py @@ -26,7 +26,7 @@ def _time_mock_side_effect(): @mock.patch(M_PATH + "LOG.debug") @mock.patch(M_PATH + "actions.auto_attach") -@mock.patch(M_PATH + "lock.SpinLock") +@mock.patch(M_PATH + "lock.RetryLock") class TestAttemptAutoAttach: def test_success( self, m_spin_lock, m_auto_attach, m_log_debug, FakeConfig diff --git a/uaclient/lock.py b/uaclient/lock.py index 18d7f058d8..5ba292ad94 100644 --- a/uaclient/lock.py +++ b/uaclient/lock.py @@ -21,14 +21,14 @@ def clear_lock_file_if_present(): clear_lock_file() -class SpinLock: +class RetryLock: """ Context manager for gaining exclusive access to the lock file. Create a lock file if absent. The lock file will contain a pid of the running process, and a customer-visible description of the lock holder. - The SpinLock will try several times to acquire the lock before giving up. + The RetryLock will try several times to acquire the lock before giving up. The number of times to try and how long to sleep in between tries is configurable. @@ -82,7 +82,7 @@ def __enter__(self): break except exceptions.LockHeldError as e: LOG.debug( - "SpinLock Attempt %d. %s. Spinning...", tries + 1, e.msg + "RetryLock Attempt %d. %s. Spinning...", tries + 1, e.msg ) tries += 1 if tries >= self.max_retries: diff --git a/uaclient/tests/test_lock.py b/uaclient/tests/test_lock.py index 5e084ffa3e..a1010418cf 100644 --- a/uaclient/tests/test_lock.py +++ b/uaclient/tests/test_lock.py @@ -3,14 +3,14 @@ from uaclient.exceptions import LockHeldError from uaclient.files.notices import Notice -from uaclient.lock import SpinLock +from uaclient.lock import RetryLock from uaclient.messages import LOCK_HELD M_PATH = "uaclient.lock." M_PATH_UACONFIG = "uaclient.config.UAConfig." -class TestSpinLock: +class TestRetryLock: @mock.patch("os.getpid", return_value=123) @mock.patch(M_PATH_UACONFIG + "delete_cache_key") @mock.patch("uaclient.files.notices.NoticesManager.add") @@ -30,7 +30,7 @@ def test_function(arg): assert arg == mock.sentinel.arg return mock.sentinel.success - with SpinLock(cfg=cfg, lock_holder="some operation"): + with RetryLock(cfg=cfg, lock_holder="some operation"): ret = test_function(arg) assert mock.sentinel.success == ret @@ -61,7 +61,7 @@ def test_function(): raise RuntimeError("test") with pytest.raises(RuntimeError) as exc: - with SpinLock(cfg=cfg, lock_holder="some operation"): + with RetryLock(cfg=cfg, lock_holder="some operation"): test_function() assert "test" == str(exc.value) @@ -76,7 +76,7 @@ def test_function(): @mock.patch(M_PATH + "time.sleep") @mock.patch( - M_PATH + "SpinLock.grab_lock", + M_PATH + "RetryLock.grab_lock", side_effect=[ LockHeldError( lock_request="request", lock_holder="holder", pid=10 @@ -92,7 +92,7 @@ def test_spins_when_lock_held( ): cfg = FakeConfig() - with SpinLock( + with RetryLock( cfg=cfg, lock_holder="request", sleep_time=1, max_retries=3 ): pass @@ -106,7 +106,7 @@ def test_spins_when_lock_held( @mock.patch(M_PATH + "time.sleep") @mock.patch( - M_PATH + "SpinLock.grab_lock", + M_PATH + "RetryLock.grab_lock", side_effect=[ LockHeldError( lock_request="request", lock_holder="holder", pid=10 @@ -123,7 +123,7 @@ def test_raises_lock_held_after_max_retries( cfg = FakeConfig() with pytest.raises(LockHeldError) as exc: - with SpinLock( + with RetryLock( cfg=cfg, lock_holder="request", sleep_time=1, max_retries=2 ): pass diff --git a/uaclient/tests/test_reboot_cmds.py b/uaclient/tests/test_reboot_cmds.py index 9712ec83f0..82bfaf074a 100644 --- a/uaclient/tests/test_reboot_cmds.py +++ b/uaclient/tests/test_reboot_cmds.py @@ -91,7 +91,7 @@ def test_fix_pro_pkg_holds( ) # noqa: E501 @mock.patch("lib.reboot_cmds.refresh_contract") @mock.patch("lib.reboot_cmds.fix_pro_pkg_holds") -@mock.patch("uaclient.lock.SpinLock") +@mock.patch("uaclient.lock.RetryLock") @mock.patch("lib.reboot_cmds._is_attached") @mock.patch( "uaclient.files.state_files.reboot_cmd_marker_file",