pkgCacheFile unlocks in destructor even if it did not acquire lock

Bug #1794053 reported by Julian Andres Klode
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
apt (Ubuntu)
Fix Released
High
Julian Andres Klode
Trusty
Triaged
High
Unassigned
Xenial
Fix Released
High
Unassigned
Bionic
Fix Released
High
Unassigned
Cosmic
Fix Released
High
Julian Andres Klode

Bug Description

[Impact]
Closing a cache file unlocks the system (loses the lock). This randomly breaks ubuntu-make, for example.

[Test case]
The following Python code fails, but should succeed, as it locks first, opens a cache and closes it, and then unlocks.

import apt_pkg, os, gc

apt_pkg.init()
apt_pkg.pkgsystem_lock()

os.system("ls -l /proc/%d/fd" % os.getpid())
apt_pkg.Cache()

gc.collect()

os.system("ls -l /proc/%d/fd" % os.getpid())

apt_pkg.pkgsystem_unlock()

[Regression potential]
This fixes a regression introduced in 2001 by

commit b2e465d6d32d2dc884f58b94acb7e35f671a87fe
Author: Arch Librarian <email address hidden>
Date: Mon Sep 20 16:56:32 2004 +0000

    Join with aliencode
    Author: jgg
    Date: 2001-02-20 07:03:16 GMT
    Join with aliencode

The fix is to only UnLock from the cache file if the cache file locked before. It's unclear how far code relies on that regression. That said, any potential regression should be easily noticable, as it would be a case of not unlocking where we were unlocking before.

Changed in apt (Ubuntu):
importance: Undecided → High
status: New → In Progress
assignee: nobody → Julian Andres Klode (juliank)
Changed in apt (Ubuntu Bionic):
importance: Undecided → High
Changed in apt (Ubuntu Xenial):
importance: Undecided → High
Changed in apt (Ubuntu Trusty):
importance: Undecided → High
status: New → Triaged
Changed in apt (Ubuntu Xenial):
status: New → Triaged
Changed in apt (Ubuntu Bionic):
status: New → Triaged
Changed in apt (Ubuntu Cosmic):
status: In Progress → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package apt - 1.7.0~rc2ubuntu1

---------------
apt (1.7.0~rc2ubuntu1) cosmic; urgency=medium

  * Fix calculation of elapsed usec in downloads
  * pkgCacheFile: Only unlock in destructor if locked before (LP: #1794053)

 -- Julian Andres Klode <email address hidden> Mon, 24 Sep 2018 12:18:26 +0200

Changed in apt (Ubuntu Cosmic):
status: Fix Committed → Fix Released
Revision history for this message
Łukasz Zemczak (sil2100) wrote : Please test proposed package

Hello Julian, or anyone else affected,

Accepted apt into bionic-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/apt/1.6.5 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested and change the tag from verification-needed-bionic to verification-done-bionic. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-bionic. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Changed in apt (Ubuntu Bionic):
status: Triaged → Fix Committed
tags: added: verification-needed verification-needed-bionic
Revision history for this message
Łukasz Zemczak (sil2100) wrote :

Hello Julian, or anyone else affected,

Accepted apt into xenial-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/apt/1.2.28 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested and change the tag from verification-needed-xenial to verification-done-xenial. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-xenial. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Changed in apt (Ubuntu Xenial):
status: Triaged → Fix Committed
tags: added: verification-needed-xenial
Revision history for this message
Brian Murray (brian-murray) wrote :

Hello Julian, or anyone else affected,

Accepted apt into xenial-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/apt/1.2.29 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested and change the tag from verification-needed-xenial to verification-done-xenial. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-xenial. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Revision history for this message
Brian Murray (brian-murray) wrote :

Hello Julian, or anyone else affected,

Accepted apt into bionic-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/apt/1.6.6 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested and change the tag from verification-needed-bionic to verification-done-bionic. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-bionic. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Revision history for this message
Julian Andres Klode (juliank) wrote :

bionic (1.6.3ubuntu0.1 -> 1.6.6):

root@dashing-panda:~# python3 a.py
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:07 3 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:07 6 -> 'pipe:[27578280]'
Reading package lists... Done
Building dependency tree
Reading state information... Done
total 0
[...]
l-wx------ 1 root root 64 Oct 18 12:07 6 -> 'pipe:[27578280]'
Traceback (most recent call last):
  File "a.py", line 13, in <module>
    apt_pkg.pkgsystem_unlock()
apt_pkg.Error: E:Not locked
[...]

=> We can see that the cache was already unlocked when it should not have been

root@dashing-panda:~# apt install libapt-pkg5.0
[...]
Unpacking libapt-pkg5.0:amd64 (1.6.6) over (1.6.3ubuntu0.1) ...
[...]
root@dashing-panda:~# python3 a.py
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:08 3 -> /var/lib/dpkg/lock-frontend
lrwx------ 1 root root 64 Oct 18 12:08 4 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:08 6 -> 'pipe:[27578280]'
Reading package lists... Done
Building dependency tree
Reading state information... Done
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:08 3 -> /var/lib/dpkg/lock-frontend
lrwx------ 1 root root 64 Oct 18 12:08 4 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:08 6 -> 'pipe:[27578280]'
root@dashing-panda:~#

=> script worked fine, we see that the lock is still open in the second ls, and there are no error messages

tags: added: verification-done-bionic
removed: verification-needed-bionic
Revision history for this message
Julian Andres Klode (juliank) wrote :

xenial
======

With 1.2.27
-----------

root@glowing-mastodon:~# python3 a.py
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:15 3 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:15 6 -> pipe:[27632799]
Reading package lists... Done
Building dependency tree
Reading state information... Done
total 0
[...]
l-wx------ 1 root root 64 Oct 18 12:15 6 -> pipe:[27632799]
Traceback (most recent call last):
  File "a.py", line 13, in <module>
    apt_pkg.pkgsystem_unlock()
SystemError: E:Not locked

=> broken as expected

With 1.2.29
-----------

root@glowing-mastodon:~# python3 a.py
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:17 3 -> /var/lib/dpkg/lock-frontend
lrwx------ 1 root root 64 Oct 18 12:17 4 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:17 6 -> pipe:[27632799]
Reading package lists... Done
Building dependency tree
Reading state information... Done
total 0
[...]
lrwx------ 1 root root 64 Oct 18 12:17 3 -> /var/lib/dpkg/lock-frontend
lrwx------ 1 root root 64 Oct 18 12:17 4 -> /var/lib/dpkg/lock
l-wx------ 1 root root 64 Oct 18 12:17 6 -> pipe:[27632799]
root@glowing-mastodon:~#

=> lock is still held at second ls, and releasing it worked fine.

tags: added: verification-done verification-done-xenial
removed: verification-needed verification-needed-xenial
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package apt - 1.6.6

---------------
apt (1.6.6) bionic; urgency=medium

  * Set DPKG_FRONTEND_LOCKED when running {pre,post}-invoke scripts.
    Some post-invoke scripts install packages, which fails because
    the environment variable is not set. This sets the variable for
    all three kinds of scripts {pre,post-}invoke and pre-install-pkgs,
    but we will only allow post-invoke at a later time.
    (LP: #1796808)

apt (1.6.5) bionic; urgency=medium

  [ David Kalnischkies ]
  * Support records larger than 32kb in 'apt show' (Closes: #905527)
    (LP: #1787120)

  [ Julian Andres Klode ]
  * Add support for dpkg frontend lock (Closes: #869546) (LP: #1781169)
  * Set DPKG_FRONTEND_LOCKED as needed when doing selection changes
  * http: Stop pipeline after close only if it was not filled before
    (LP: #1794957)
  * pkgCacheFile: Only unlock in destructor if locked before (LP: #1794053)
  * Update libapt-pkg5.0 symbols for frontend locking

 -- Julian Andres Klode <email address hidden> Tue, 09 Oct 2018 12:16:51 +0200

Changed in apt (Ubuntu Bionic):
status: Fix Committed → Fix Released
Revision history for this message
Łukasz Zemczak (sil2100) wrote : Update Released

The verification of the Stable Release Update for apt has completed successfully and the package has now been released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package apt - 1.2.29

---------------
apt (1.2.29) xenial; urgency=medium

  * Set DPKG_FRONTEND_LOCKED when running {pre,post}-invoke scripts.
    Some post-invoke scripts install packages, which fails because
    the environment variable is not set. This sets the variable for
    all three kinds of scripts {pre,post-}invoke and pre-install-pkgs,
    but we will only allow post-invoke at a later time.
    (LP: #1796808)

apt (1.2.28) xenial; urgency=medium

  [ Julian Andres Klode ]
  * apt.conf.autoremove: Add linux-cloud-tools to list (LP: #1698159)
  * Add support for dpkg frontend lock (Closes: #869546) (LP: #1781169)
  * Set DPKG_FRONTEND_LOCKED as needed when doing selection changes
  * http: Stop pipeline after close only if it was not filled before
    (LP: #1794957)
  * pkgCacheFile: Only unlock in destructor if locked before (LP: #1794053)
  * Update libapt-pkg5.0 symbols for frontend locking

  [ David Kalnischkies ]
  * Support records larger than 32kb in 'apt show' (Closes: #905527)
    (LP: #1787120)

 -- Julian Andres Klode <email address hidden> Tue, 09 Oct 2018 12:35:03 +0200

Changed in apt (Ubuntu Xenial):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.