feat(pacman): close the manager holding the database lock instead of waiting
tests / version-guard (push) Has been cancelled
tests / build-test (push) Has been cancelled

A locked database ended in "Un autre gestionnaire de paquets utilise déjà la
base. Attendez la fin de l'autre opération puis réessayez": the app knew
something else was running but could not say what, leaving the user to find
and close it before retrying by hand.

The lock is now inspected: the process holding /var/lib/pacman/db.lck is
identified through /proc (with a pgrep fallback for the known managers, whose
fd table is unreadable when they run as root), the dialog names it, and one
click stops it — SIGTERM first, so pacman aborts its transaction and releases
the lock itself, SIGKILL only when it does not react — then the pending
operation resumes on its own. A lock whose owner is gone is dropped the same
way, and one that disappears while the action is unwinding is retried without
a dialog at all.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-12 22:47:53 +02:00
1 parent d8bc17b2aa
commit fb5c11e153
7 files changed
+517 -28

No files matched your search

+65
View File
@@ -1,6 +1,13 @@
#include "pacman-manager.h"
#include <cassert>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
static void testParseInstalledOutput_parsesSimpleLines() {
std::string output = "firefox 128.0\nvim 9.1.0000\n";
@@ -183,6 +190,61 @@ static void testAuthFailed_ignoresPacmanErrors() {
assert(!PacmanManager::authFailed("warning: firefox is up to date"));
}
// The lock dialog names the process that holds pacman's database, so the
// description has to cover the three states the detection can report.
static void testDescribeLockHolder() {
PacmanManager::LockHolder holder;
assert(PacmanManager::describeLockHolder(holder).empty());
holder.lockFilePresent = true;
holder.stale = true;
assert(PacmanManager::describeLockHolder(holder).find("résiduel")
!= std::string::npos);
holder.stale = false;
holder.pid = 4321;
holder.name = "pamac-manager";
assert(PacmanManager::describeLockHolder(holder) == "pamac-manager (PID 4321)");
// A process whose name could not be read is still worth reporting.
holder.name.clear();
assert(PacmanManager::describeLockHolder(holder).find("4321") != std::string::npos);
}
// Detection must name the process actually holding the file open, the way
// pacman keeps db.lck during a transaction.
static void testLockHolderPid_findsTheHolder() {
char pathTemplate[] = "/tmp/pacmanager-lock-test-XXXXXX";
const int fd = mkstemp(pathTemplate);
assert(fd >= 0);
close(fd);
const std::string lockPath = pathTemplate;
const pid_t pid = fork();
if (pid == 0) {
// Child: hold the file open, exactly like a running transaction does.
const std::string script = "exec 9<'" + lockPath + "'; sleep 30";
execl("/bin/sh", "sh", "-c", script.c_str(), static_cast<char*>(nullptr));
_exit(127);
}
assert(pid > 0);
// The child needs a moment to reach the redirection.
int found = 0;
for (int attempt = 0; attempt < 50 && found != static_cast<int>(pid); ++attempt) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
found = PacmanManager::lockHolderPid(lockPath);
}
assert(found == static_cast<int>(pid));
kill(pid, SIGTERM);
waitpid(pid, nullptr, 0);
// Nobody holds it any more: that is what makes the lock look stale.
assert(PacmanManager::lockHolderPid(lockPath) == 0);
unlink(lockPath.c_str());
}
static void testDbLocked_detectsEnglishLock() {
assert(PacmanManager::dbLocked(
"error: failed to synchronize all databases (unable to lock database)"));
@@ -238,6 +300,9 @@ int main() {
testAuthFailed_detectsMissingPassword();
testAuthFailed_ignoresPacmanErrors();
testDescribeLockHolder();
testLockHolderPid_findsTheHolder();
testDbLocked_detectsEnglishLock();
testDbLocked_detectsFrenchLock();
testDbLocked_detectsLockFile();