0

Please note: there're similar questions around, but this specific scenario is unanswered. Don't close this as a duplicate of 335794 or 1340943.

This is always reproducible, and this is about apt lists lock not about dpkg lock.

I'm running a series of automated tasks that perform some install through APT. The tasks are sequential (zero parallelism) but may invoke apt multiple times. At a certain point, when the script executes

sudo apt -o DPkg::Lock::Timeout=60 update

I immeditately get this error output:

Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1496 (packagekitd)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/

this error goes away quite quickly - if I retry after 10 seconds the operation (usually) succeeds, but sometimes it stays locked longer because packagekitd is doing who-knows-what.

My problem: this is an unattended operation. I'd like to have this automated, but I couldn't find a way to either:

  • tell apt to wait until the lists lock is available, or
  • to manually wait for the lock - I tried using flock even with an exclusive lock, but acquiring it seems to always succeed (it's probably using another locking approach)

Surely I could sleep every time. But this is flaky and it will makes the process quite slow.

OS: Ubuntu 22.04 Intel 64bit

4
  • 1
    Did you try askubuntu.com/a/373478/158442? Or the aptdcon mentioned in the other answers there?
    – muru
    Jan 24, 2023 at 11:39
  • Seems like a possible race condition. Is this an automated new-install? Or does the same issue occur on old installs when you run the script?
    – user535733
    Jan 24, 2023 at 12:37
  • It cannot be a race condition. All tasks are sequentials. If something "races" is something that packagekitd is doing on its own. The same issue happens every time I launch the script. Jan 24, 2023 at 14:48
  • @muru the fuser approach worked. Jan 25, 2023 at 20:16

1 Answer 1

0
#!/bin/sh

while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do
   sleep 1
done

This worked. From How to make a package manager wait if another instance of APT is running?

Not the answer you're looking for? Browse other questions tagged .