Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmupdate: fix waiting for the other apt-get process #175

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions vmupdate/agent/source/apt/apt_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import fcntl
import os
import contextlib
from typing import List

from source.common.package_manager import PackageManager
Expand All @@ -38,12 +39,21 @@ def __init__(self, log_handler, log_level,):
# to prevent a warning: `debconf: unable to initialize frontend: Dialog`
os.environ['DEBIAN_FRONTEND'] = 'noninteractive'

@contextlib.contextmanager
def apt_lock(self):
"""
Contex manager for locking compatible with 'apt-get update' lock.
"""
with open("/var/lib/apt/lists/lock", "rb+") as f_lock:
fcntl.lockf(f_lock.fileno(), fcntl.LOCK_EX)
yield

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)
with self.apt_lock():
pass

def refresh(self, hard_fail: bool) -> ProcessResult:
"""
Expand All @@ -52,9 +62,12 @@ 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()
cmd = [self.package_manager, "-q", "update"]
result = self.run_cmd(cmd)
# apply lock externally to wait for it, until
# https://bugs.debian.org/1069167 gets implemented
with self.apt_lock():
cmd = [self.package_manager,
"-o", "Debug::NoLocking=true", "-q", "update"]
result = self.run_cmd(cmd)
# 'apt-get update' reports error with exit code 100, but updater as a
# whole reserves it for "no updates"
if result.code == 100:
Expand Down