Skip to content

Commit 8fa2929

Browse files
committed
vmupdate: wait for other apt-get to complete
If there is an `apt-get update` running in the background (for example periodical updates check), running another would fail. Since `apt-get` itself doesn't have an option to wait for the lock, wait for it in the updater tool. It's not perfect - there is still a window where another apt-get instance can start, but at least this will handle the (more common) situation when it's already running.
1 parent 79756e7 commit 8fa2929

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

vmupdate/agent/source/apt/apt_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def refresh(self, hard_fail: bool) -> ProcessResult:
5353
:param hard_fail: raise error if some repo is unavailable
5454
:return: (exit_code, stdout, stderr)
5555
"""
56+
self.wait_for_lock()
5657
result = ProcessResult()
5758
try:
5859
self.log.debug("Refreshing available packages...")

vmupdate/agent/source/apt/apt_cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
# pylint: disable=unused-argument
2323

24+
import fcntl
2425
import os
2526
from typing import List
2627

@@ -37,13 +38,21 @@ def __init__(self, log_handler, log_level,):
3738
# to prevent a warning: `debconf: unable to initialize frontend: Dialog`
3839
os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
3940

41+
def wait_for_lock(self):
42+
"""
43+
Wait for any other apt-get instance to finish.
44+
"""
45+
with open("/var/lib/apt/lists/lock") as f_lock:
46+
fcntl.flock(f_lock.fileno(), fcntl.LOCK_EX)
47+
4048
def refresh(self, hard_fail: bool) -> ProcessResult:
4149
"""
4250
Use package manager to refresh available packages.
4351
4452
:param hard_fail: raise error if some repo is unavailable
4553
:return: (exit_code, stdout, stderr)
4654
"""
55+
self.wait_for_lock()
4756
cmd = [self.package_manager, "-q", "update"]
4857
result = self.run_cmd(cmd)
4958
# 'apt-get update' reports error with exit code 100, but updater as a

0 commit comments

Comments
 (0)