Skip to content

Commit

Permalink
vmupdate: wait for other apt-get to complete
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marmarek committed Dec 15, 2024
1 parent 79756e7 commit 8fa2929
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions vmupdate/agent/source/apt/apt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def refresh(self, hard_fail: bool) -> ProcessResult:
:param hard_fail: raise error if some repo is unavailable
:return: (exit_code, stdout, stderr)
"""
self.wait_for_lock()
result = ProcessResult()
try:
self.log.debug("Refreshing available packages...")
Expand Down
9 changes: 9 additions & 0 deletions vmupdate/agent/source/apt/apt_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# pylint: disable=unused-argument

import fcntl
import os
from typing import List

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

def wait_for_lock(self):
"""
Wait for any other apt-get instance to finish.
"""
with open("/var/lib/apt/lists/lock") as f_lock:
fcntl.flock(f_lock.fileno(), fcntl.LOCK_EX)

def refresh(self, hard_fail: bool) -> ProcessResult:
"""
Use package manager to refresh available packages.
:param hard_fail: raise error if some repo is unavailable
:return: (exit_code, stdout, stderr)
"""
self.wait_for_lock()
cmd = [self.package_manager, "-q", "update"]
result = self.run_cmd(cmd)
# 'apt-get update' reports error with exit code 100, but updater as a
Expand Down

0 comments on commit 8fa2929

Please sign in to comment.