OPINIONATED OMARCHY
TROUBLESHOOTING CORPUS
790
RECORDS
790
AUDITED
0
UNCHECKED
2388
SOURCES
LIVE
pacman-aur medium very-common DISPUTE

Clear a stale pacman lock after 'unable to lock database'

archomarchyendeavouroscachyosmanjarodesktoplaptop
CORRECTED An auditor found the fix wrong or incomplete and rewrote it.
AUDIT NOTE

Lock path confirmed on this machine: pacman -Qv prints Lock File : /var/lib/pacman/db.lck on pacman 7.1.0 (libalpm 16.0.1), and the Arch wiki Pacman page section 'Failed to init transaction (unable to lock database)' gives the same path, the rm, and the fuser tip. Mechanism confirmed from pacman source (lib/libalpm/handle.c _alpm_handle_lock: open(O_WRONLY|O_CREAT|O_EXCL), fd kept open for the transaction, unlinked in alpm_unlock, and src/pacman/util.c prints the 'if you're sure a package manager is not already running, you can remove' hint). The cause is correct. Two things were wrong for Omarchy 4. First, the fix and the verify both run sudo pacman -Syu, which /usr/share/libalpm/hooks/00-omarchy-update-guard.hook (PreTransaction, AbortOnFail, read on this machine) aborts whenever the pacman command line carries both -S and -u without OMARCHY_UPDATE_PACMAN=1 or OMARCHY_ALLOW_DIRECT_PACMAN=1, so on Omarchy the recovery command fails with 'Woah partner'. Second, the record folds omarchy-update into the pacman lock case, but omarchy update holds its own lock: /usr/share/omarchy/bin/omarchy-update-lock (read here, omarchy 4.0.2-1) takes a kernel flock -n on $XDG_RUNTIME_DIR/omarchy-update.lock, which the kernel releases when the holder dies, so that lock cannot go stale and 'An Omarchy update is already running.' always means a live process. That is a distinct case and the corrected fix separates it. Also checked: omarchy-update-available runs checkupdates, which uses its own --dbpath copy and never takes db.lck, so the update notifier is not a lock holder. fuser is present (psmisc 23.7-2). Symptom text was tidied to the real two-line pacman message. Not exercised: no stale lock was created or removed on this machine (no sudo), so the fuser and rm steps are confirmed from source and the wiki only.

The cause above was not rewritten and may still contain the error described. The fix below is the corrected version.
RISK

Deleting db.lck while pacman really is running will let a second instance write the database at the same time and can corrupt /var/lib/pacman/local, leaving an unrecoverable package database. Always run fuser first.

Symptom

pacman refuses to do anything:


error: failed to init transaction (unable to lock database)
error: could not lock database: File exists
  if you're sure a package manager is not already
  running, you can remove /var/lib/pacman/db.lck

Often happens after closing a terminal mid-update, after a crash or power loss, or while another package manager (yay, paru, a GUI updater, or omarchy update) is still running in the background. On Omarchy 4 a second omarchy update prints a different message, An Omarchy update is already running., which is not a pacman lock at all (see the fix).

Cause

pacman creates /var/lib/pacman/db.lck before it alters the package database, so two instances cannot write at once. If pacman is killed or the machine loses power mid-transaction, the lock file is left behind and every later run refuses to start.

Fix

First confirm nothing is actually using the lock. This is the step people skip and it is why systems get corrupted. pacman keeps the lock file open for the whole transaction, so fuser sees the holder (it needs sudo because pacman runs as root):


sudo fuser /var/lib/pacman/db.lck
# also check for a running package manager
ps aux | grep -E '[p]acman|[y]ay|[p]aru|[o]marchy-update'

If fuser DOES print a PID, wait for that process to finish instead of deleting the file. On Omarchy, omarchy update runs pacman as sudo env OMARCHY_UPDATE_PACMAN=1 pacman -Syu, so it shows up in that ps output, and its transcript is in /tmp/omarchy-update.log.

If fuser prints nothing and no package manager process exists, delete the stale lock and rerun the upgrade through the supported path.

**Omarchy 4:**


sudo rm /var/lib/pacman/db.lck
omarchy update

Do not rerun with sudo pacman -Syu here: the 00-omarchy-update-guard.hook pacman hook aborts any direct -Syu with Woah partner.... If you really want one direct transaction:


sudo env OMARCHY_ALLOW_DIRECT_PACMAN=1 pacman -Syu

**Plain Arch:**


sudo rm /var/lib/pacman/db.lck
sudo pacman -Syu

**An Omarchy update is already running. is a different lock.** omarchy update takes a kernel flock on $XDG_RUNTIME_DIR/omarchy-update.lock (normally /run/user/<uid>/omarchy-update.lock) through omarchy-update-lock. The kernel drops that lock when the holding process exits, so it cannot be left behind by a crash and there is nothing to delete. The message means an update is genuinely still running. Find it and watch it:


pgrep -af omarchy-update
tail -f /tmp/omarchy-update.log

If pgrep finds nothing, the lock is already free and omarchy update will start.

Verify

On Omarchy, omarchy update reaches Update system packages and pacman starts resolving dependencies instead of printing unable to lock database. On plain Arch, sudo pacman -Syu starts resolving. In both cases ls /var/lib/pacman/db.lck reports no such file once pacman is idle.

Sources

pacman-unable-to-lock-database