From 61b85ed8a43971adba30be602902a3efb96c4ce7 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 4 Aug 2026 18:25:37 +0200 Subject: feat(sync): notice syncs this window did not start The user's cron runs mailsync.sh every ten minutes, so mail arrives and tags change while the window sits idle, and nothing here noticed. The script already holds an flock for the whole run, so that lock is the signal: no status file is needed, and a kernel lock cannot go stale because it dies with the process holding it. The observation method is the part that matters, and two of the three plausible ones are wrong. Both were probed on Linux 6.18 before any of this was written: - flock -n acquires in order to test, so polling every two seconds would open a window every two seconds in which a starting mailsync.sh is refused the lock and exits 75. It would cause the very skips the script reports. - fcntl(F_OFD_GETLK) never acquires and looks ideal, but reports UNLOCKED against a lock held by flock(2): separate lock namespaces in the kernel, which cannot see each other. A silent false negative. - /proc/locks is a pure read. It observes flock(2) correctly, and since it takes no lock at all it can never contend with the Xapian write lock notmuch new holds during the same run. Confirmed: 200 reads left the lock table unchanged and this process holding nothing. SyncMonitor keeps the parsing separate from the polling so the parsing is testable, and it reports Unknown rather than Idle where /proc/locks cannot be read: "no sync is running" is the claim that would let the window quit, so it must never be guessed. Verified against a real flock end to end, not only against synthetic content. It reports rather than refreshes. runCurrentQuery() clears the undo stack, the selection and the message pane, which is right for a query the user typed and hostile for one a cron timer fired: it would discard undo history and close the thread being read up to six times an hour, with no action from the user. Co-Authored-By: Claude Opus 5 --- src/mainwindow.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/mainwindow.cpp') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b3b67c5..7d0496d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -431,6 +431,15 @@ void MainWindow::buildUi() m_syncLog->appendPlainText(chunk.trimmed()); }); + // Syncs this window did not start. The user's cron runs the same script + // every ten minutes, so mail arrives and tags change while the window sits + // idle, and until now nothing here noticed. + m_syncMonitor = new SyncMonitor(SyncMonitor::defaultLockPath(), + QStringLiteral("/proc/locks"), this); + connect(m_syncMonitor, &SyncMonitor::stateChanged, + this, &MainWindow::onExternalSyncStateChanged); + m_syncMonitor->start(); + queryRow->addWidget(m_accountBox); queryRow->addWidget(m_queryEdit, 1); queryRow->addWidget(m_syncButton); @@ -1289,6 +1298,38 @@ void MainWindow::onTagsApplied(const TagChange &change) } } +void MainWindow::onExternalSyncStateChanged(SyncMonitor::State state) +{ + // A sync this window started is already reported by setSyncBusy(), and the + // monitor sees that lock too. Saying so twice would fight over the status + // bar and would re-enable the progress bar as the local run finished. + if (m_sync && m_sync->isRunning()) + return; + + if (state == SyncMonitor::State::Running) { + m_syncProgress->setVisible(true); + m_statusLabel->setText(tr("Syncing (started elsewhere)...")); + return; + } + + m_syncProgress->setVisible(false); + + // Deliberately reports rather than refreshes. runCurrentQuery() clears the + // undo stack, the selection and the message pane, which is right for a + // query the user typed and hostile for one fired by a cron timer: with a + // sync every ten minutes it would discard undo history and close the thread + // being read, up to six times an hour, with no action from the user. + // + // Unknown is not worth reporting either. It means the lock table could not + // be read, so nothing was observed, and "sync finished" would be a claim + // this cannot support. + if (state == SyncMonitor::State::Idle) { + m_statusLabel->setText( + tr("Sync finished elsewhere. Press Enter in the query bar to " + "refresh.")); + } +} + void MainWindow::setSyncBusy(bool busy) { m_syncProgress->setVisible(busy); -- cgit v1.2.3