VM extenction failing with error Could not get lock /var/lib/apt/lists/lock

Kishore 21 Reputation points
2021-05-17T07:30:42.88+00:00

We have a extension script which installs some apt packages. Our issue is some times extension getting success but some times failing with error Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)\nE: Unable to lock directory /var/lib/apt/lists/\nE: Unable to locate package atop

our observation was Extension failing to run apt-get update command, so required packages are not available to install.

Can any one help on the same?

Thanks,
Kishore

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,018 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-05-17T20:29:36.673+00:00

    Hello anonymous user-0983 ,
    Thanks for your query ! You might want to take a closer look at your custom script and the sequence of commands to track the process which is holding the lock.
    There seems to be an issue with a race condition where something wrong with one of the command in custom script as a part of apt-get update . There might be some other applications/process using the apt. (or previous apt process is still running in the background)

    You can use below command to find what process is using the apt:-
    ps aux | grep '[a]pt'
    (or)
    ps aux | grep -i apt
    (it will tell you the process name/ID which is holding the lock)

    Just to debug , try to add an additional command like below in your script to find out the locks
    sudo fuser /var/lib/apt/lists/lock

    Do you know what that script does and how frequently it runs ?
    You can try a possible workaround of adding a retry logic to the custom script temporarily to handle the error and retry after lock is gone.
    Also , when it happens you might want to add a command to delete that lock file using the below command:
    sudo rm -rf /var/lib/apt/lists/lock

    Also in your script , check out the options/parameters passed to apt upgrade

    Hope above information helps out in finding the issue !

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.