Skip to content

Commit

Permalink
[Build]: Fix dpkg lock (#15735)
Browse files Browse the repository at this point in the history
Fix #15722

We don't need to use a lock file every time we call dpkg.
For some commands, like dpkg --print-architecture or dpkg --compare-versions, this action is not required.
Moreover, sometimes these commands are called by apt-get and dpkg -i, and we get a deadlock in this case.

How I did it
Use dpkg lock only for dpkg commands that use /var/lib/dpkg/lock or /var/lib/dpkg/lock-frontend.
I mean dpkg -i, dpkg -P, ...

How to verify it
Check there is no dpkg lock errors in build log.
Check build time of docker-sonic-vs.gz:

old:
08:57:14[ building ] [ target/docker-sonic-vs.gz ]
09:12:47 [ finished ] [ target/docker-sonic-vs.gz ]

new:
01:45:12[ building ] [ target/docker-sonic-vs.gz ]
01:50:39 [ finished ] [ target/docker-sonic-vs.gz ]
  • Loading branch information
k-v1 committed May 19, 2024
1 parent 881abd3 commit 60b1440
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/sonic-build-hooks/hooks/dpkg
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

. /usr/local/share/buildinfo/scripts/buildinfo_base.sh
REAL_COMMAND=$(get_command dpkg)
COMMAND_INFO="Locked by command: $REAL_COMMAND $@"
NEED_RELEASE_LOCK=n
if [[ "$DPKG_HOOK_LOCKED" != "y" ]];then
COMMAND_INFO="Locked by command: $REAL_COMMAND $*"
DPKG_NEED_LOCK=$(check_dpkg_need_lock "$@")

if [ "$DPKG_NEED_LOCK" == "y" ]; then
lock_result=$(acquire_apt_installation_lock "$COMMAND_INFO" )
export DPKG_HOOK_LOCKED=y
NEED_RELEASE_LOCK=y
fi

$REAL_COMMAND "$@"
command_result=$?
if [[ "$NEED_RELEASE_LOCK" == "y" ]];then
unset DPKG_HOOK_LOCKED
fi
[ "$lock_result" == y ] && release_apt_installation_lock
exit $command_result
33 changes: 33 additions & 0 deletions src/sonic-build-hooks/scripts/buildinfo_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,39 @@ check_apt_install()
done
}

# Check if we need to use apt_installation_lock for this dpkg command
check_dpkg_need_lock()
{
for para in "$@"
do
if [ "$para" == "-i" ] || [ "$para" == "--install" ]; then
echo y
break
fi

if [ "$para" == "-P" ] || [ "$para" == "--purge" ]; then
echo y
break
fi

if [ "$para" == "-r" ] || [ "$para" == "--remove" ]; then
echo y
break
fi

if [ "$para" == "--unpack" ] || [ "$para" == "--configure" ]; then
echo y
break
fi

if [ "$para" == "--update-avail" ] || [ "$para" == "--merge-avail" ] || [ "$para" == "--clear-avail" ]; then
echo y
break
fi

done
}

# Print warning message if a debian package version not specified when debian version control enabled.
check_apt_version()
{
Expand Down

0 comments on commit 60b1440

Please sign in to comment.