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

+153 -27
View File
@@ -125,7 +125,20 @@ InstallWorker::InstallWorker(IPackageManager* pm, Op op,
InstallWorker::InstallWorker(const std::vector<IPackageManager*>& providers, Op op)
: m_providers(providers), m_op(op) {}
InstallWorker::InstallWorker(PacmanManager* pacman, const PacmanManager::LockHolder& holder)
: m_op(Op::ReleaseLock), m_pacman(pacman), m_lockHolder(holder) {}
void InstallWorker::run() {
// Lock recovery bypasses the provider loop: it is one pacman-side operation
// whose progress comes back through the same signal as an install.
if (m_op == Op::ReleaseLock) {
auto cb = [this](const std::string& message, int) {
emit progress(QString::fromStdString(message));
};
emit finished(m_pacman && m_pacman->releaseDatabaseLock(m_lockHolder, cb));
return;
}
bool success = true;
for (IPackageManager* pm : m_providers) {
// Prefix progress lines with the provider when several run at once.
@@ -152,6 +165,7 @@ void InstallWorker::run() {
success = pm->removePackages(m_packages, cb) && success;
break;
case Op::CleanCache: success = pm->cleanCache(cb) && success; break;
case Op::ReleaseLock: break; // handled before the provider loop
}
}
emit finished(success);
@@ -1469,36 +1483,121 @@ IPackageManager* MainWindow::providerFor(const std::string& id) const {
bool MainWindow::showProviderError(const QString& title, IPackageManager* provider) {
if (!provider) return false;
std::string err = provider->lastError();
const std::string err = provider->lastError();
if (err.empty()) return false;
const bool dbLock = err.find("verrouillée") != std::string::npos
|| err.find("db.lck") != std::string::npos;
if (!dbLock) {
QMessageBox::warning(this, title, QString::fromStdString(err));
return false;
// A locked database is the one failure the app can resolve on its own, so it
// gets its own dialog instead of a message the user has to act on by hand.
const bool lockFailure =
(provider == m_pacman && m_pacman && m_pacman->lastErrorWasLock())
|| PacmanManager::dbLocked(err);
if (lockFailure) return handleDatabaseLock(title);
QMessageBox::warning(this, title, QString::fromStdString(err));
return false;
}
bool MainWindow::handleDatabaseLock(const QString& title) {
if (!m_pacman) return false;
// The other manager frequently finishes while the failed operation was
// unwinding: retry straight away rather than announcing a lock that is gone.
if (PacmanManager::databaseIsFree()) {
statusBar()->showMessage("Base pacman libérée, nouvelle tentative...");
QTimer::singleShot(0, this, [this] { retryPendingAction(); });
return true;
}
// Locked DB: explain + offer a retry, since the other manager (or stale
// lock) is often gone by the time the user clicks again.
const PacmanManager::LockHolder holder = PacmanManager::detectLockHolder();
const QString holderName =
QString::fromStdString(PacmanManager::describeLockHolder(holder));
QMessageBox box(this);
box.setWindowTitle(title);
box.setIcon(QMessageBox::Warning);
box.setText(QString::fromStdString(err));
box.setInformativeText("Fermez l'autre gestionnaire (ou supprimez le "
"fichier résiduel indiqué), puis réessayez.");
QPushButton* retryBtn = box.addButton("Réessayer", QMessageBox::AcceptRole);
box.addButton("Fermer", QMessageBox::RejectRole);
box.exec();
if (box.clickedButton() == retryBtn && !m_pendingRetryAction.isEmpty()) {
const QString action = m_pendingRetryAction;
m_pendingRetryAction.clear();
// Defer so the dialog closes before the worker touches the UI.
QTimer::singleShot(0, this, [this, action] { retryPrivilegedAction(action); });
box.setTextFormat(Qt::RichText);
// The action only promises a resume when there is one to replay: a lock hit
// by an operation with no pending retry just needs the database freed.
const bool canRetry = !m_pendingRetryAction.isEmpty();
const QString resume = canRetry
? QStringLiteral(" PacManager reprendra ensuite l'opération automatiquement.")
: QString();
if (holder.pid > 0) {
box.setText(QString("<b>%1</b> utilise déjà la base pacman.").arg(holderName));
box.setInformativeText(
"Le fermer libère la base ; une transaction en cours sera "
"interrompue." + resume);
} else {
m_pendingRetryAction.clear();
box.setText(QString("La base pacman est verrouillée par %1.").arg(holderName));
box.setInformativeText(
"Aucun gestionnaire ne tourne : le verrou peut être supprimé sans "
"risque." + resume);
}
QPushButton* resolveBtn = box.addButton(
holder.pid > 0 ? (canRetry ? QStringLiteral("Fermer et réessayer")
: QStringLiteral("Fermer le gestionnaire"))
: (canRetry ? QStringLiteral("Supprimer et réessayer")
: QStringLiteral("Supprimer le verrou")),
QMessageBox::AcceptRole);
QPushButton* retryBtn =
box.addButton(QStringLiteral("Réessayer"), QMessageBox::ActionRole);
box.addButton(QStringLiteral("Plus tard"), QMessageBox::RejectRole);
box.exec();
if (box.clickedButton() == retryBtn) {
QTimer::singleShot(0, this, [this] { retryPendingAction(); });
return true;
}
if (box.clickedButton() != resolveBtn) {
m_pendingRetryAction.clear();
return true;
}
// Stopping another user's manager (or removing a root-owned lock file) needs
// the sudo password, which the session may not hold yet.
if (!ensureCredentials({m_pacman})) return true;
// Deferred so the dialog closes before the worker touches the UI.
QTimer::singleShot(0, this, [this, holder] { startLockRecovery(holder); });
return true;
}
void MainWindow::startLockRecovery(const PacmanManager::LockHolder& holder) {
if (!m_pacman) return;
setBusy(true);
statusBar()->showMessage("Libération de la base de données pacman...");
auto* worker = new InstallWorker(m_pacman, holder);
connect(worker, &InstallWorker::progress, this, [this](const QString& msg) {
statusBar()->showMessage(msg);
});
connect(worker, &InstallWorker::finished, this, [this](bool released) {
setBusy(false);
if (!released) {
statusBar()->showMessage("Base pacman toujours verrouillée");
QMessageBox::warning(this, "Base de données verrouillée",
QString::fromStdString(m_pacman ? m_pacman->lastError() : std::string()));
m_pendingRetryAction.clear();
return;
}
statusBar()->showMessage("Base libérée, reprise de l'opération...");
retryPendingAction();
});
connect(worker, &InstallWorker::finished, this,
[worker] { worker->wait(); worker->deleteLater(); });
worker->start();
}
void MainWindow::retryPendingAction() {
if (m_pendingRetryAction.isEmpty()) return;
const QString action = m_pendingRetryAction;
m_pendingRetryAction.clear();
retryPrivilegedAction(action);
}
void MainWindow::retryPrivilegedAction(const QString& action) {
if (action == "install" && !m_selectedPackage.name.empty()) {
m_pendingSelect = m_selectedPackage.name;
@@ -1512,6 +1611,21 @@ void MainWindow::retryPrivilegedAction(const QString& action) {
onRefresh();
} else if (action == "update" && !m_selectedPackage.name.empty()) {
onUpdate();
} else if (action == "cleanup") {
// The groups still queued are replayed; the one that failed on the lock
// is gone, and the recovery already freed the database.
std::vector<IPackageManager*> providers;
for (const auto& entry : m_pendingRemovals) providers.push_back(entry.first);
if (!tryAcquireOp()) return;
if (!ensureCredentials(providers)) {
setBusy(false);
return;
}
startNextCleanupRemoval();
} else if (action == "clean-caches") {
onCleanCaches();
} else if (action == "install-plugin-tool") {
onInstallPluginTool();
}
}
@@ -2301,6 +2415,8 @@ void MainWindow::removeCleanupPackages(const std::vector<PackageInfo>& packages,
return;
}
// Lets a database-lock recovery re-run the removal it interrupted.
m_pendingRetryAction = "cleanup";
startNextCleanupRemoval();
}
@@ -2332,18 +2448,25 @@ void MainWindow::startNextCleanupRemoval() {
connect(worker, &InstallWorker::progressValue, this, &MainWindow::onInstallProgress);
connect(worker, &InstallWorker::finished, this,
[this, provider, packages](bool success) {
if (success) {
statusBar()->showMessage(
QString("%1 paquet(s) supprimé(s)").arg(packages.size()));
} else {
statusBar()->showMessage("Échec du nettoyage");
showProviderError("Échec du nettoyage", provider);
}
// One history row per removed package keeps the install/remove filters
// of the History page meaningful.
for (const auto& name : packages) {
m_historyManager->record({"", "remove", name, provider->getName(), success});
}
if (!success) {
// Stop the chain and keep the group queued, so the failure handler
// can resume the whole cleanup once the cause is gone (typically a
// locked database, freed through retryPendingAction()).
setBusy(false);
m_pendingRemovals.insert(m_pendingRemovals.begin(), {provider, packages});
statusBar()->showMessage("Échec du nettoyage");
showProviderError("Échec du nettoyage", provider);
return;
}
statusBar()->showMessage(
QString("%1 paquet(s) supprimé(s)").arg(packages.size()));
startNextCleanupRemoval();
});
connect(worker, &InstallWorker::finished, this, [worker] { worker->wait(); worker->deleteLater(); });
@@ -2377,6 +2500,7 @@ void MainWindow::onCleanCaches() {
return;
}
m_pendingRetryAction = "clean-caches";
statusBar()->showMessage("Nettoyage des caches...");
auto* worker = new InstallWorker(providers, InstallWorker::Op::CleanCache);
@@ -2838,6 +2962,8 @@ void MainWindow::onInstallPluginTool() {
if (installHint.empty()) return;
if (!ensureCredentials({m_pacman})) return;
// Lets a database-lock recovery re-run the tool installation.
m_pendingRetryAction = "install-plugin-tool";
setBusy(true);
statusBar()->showMessage(
QString("Installation de l'outil %1...").arg(QString::fromStdString(installHint)));