From 17abb37d291e302c033dc8007981c627a59e4f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20Gonz=C3=A1les=20D=C3=ADaz?= Date: Thu, 12 Nov 2026 21:26:16 -0500 Subject: [PATCH 1/4] Give folder listings their own thread pool and make showItem invokable Listings, folder counts, previews and descriptions all ran on the global thread pool. Counting huge or slow subfolders could occupy every thread and queue the listing of a folder just opened behind them, so clicks seemed to do nothing until a few retries overtook the stuck work. Listings now run on a dedicated pool, and folder counts on a small separate one. Backend::showItem was missing Q_INVOKABLE, so clicking a file in the columns view threw "Property 'showItem' ... is not a function". Co-Authored-By: Claude Sonnet 5.5 --- src/backend.h | 2 +- src/foldermodel.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/backend.h b/src/backend.h index 7d0f582d..51c3683e 100644 --- a/src/backend.h +++ b/src/backend.h @@ -102,7 +102,7 @@ class Backend : public QObject { // Called for org.freedesktop.FileManager1: open a folder, or open the // folder holding a file and select it. void showFolder(const QString &path); - void showItem(const QString &path); + Q_INVOKABLE void showItem(const QString &path); static QString expandTyped(const QString &text, const QString &home, const QString ¤t); static QString complete(const QString &text, const QString &home, const QString ¤t); diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 2cce5a3f..83375e10 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -40,6 +40,27 @@ FolderModel::~FolderModel() { *m_latest = 0; } +// Folder listings run on a pool of their own, apart from the folder counts +// and previews on the global one: slow or huge subfolders being counted can't +// then hold up the listing of the folder just opened. +static QThreadPool &listingPool() { + static QThreadPool *const pool = [] { + auto *p = new QThreadPool; + p->setMaxThreadCount(8); + return p; + }(); + return *pool; +} + +static QThreadPool &countingPool() { + static QThreadPool *const pool = [] { + auto *p = new QThreadPool; + p->setMaxThreadCount(2); + return p; + }(); + return *pool; +} + // Recent listings, shared by every model (the window's folder and each // column), so stepping back and forth or opening a column for a folder just // seen shows it in the same frame instead of an old or empty one. Each is @@ -481,7 +502,7 @@ void FolderModel::reload() { remember(); countFolders(generation); }); - watcher->setFuture(QtConcurrent::run([path, showHidden, sort, query, items, latest, generation, locale] { + watcher->setFuture(QtConcurrent::run(&listingPool(), [path, showHidden, sort, query, items, latest, generation, locale] { QString error; const auto overtaken = [&] { return *latest != generation; }; auto entries = isPluginView(path) ? listItems(items, query) @@ -524,7 +545,7 @@ void FolderModel::countFolders(quint64 generation) { remember(); }); const auto latest = m_latest; - watcher->setFuture(QtConcurrent::run([folders, showHidden, latest, generation]() mutable { + watcher->setFuture(QtConcurrent::run(&countingPool(), [folders, showHidden, latest, generation]() mutable { // Overtaken (another folder, or a re-read), the counts would be // thrown away, so it stops instead of listing every subfolder. for (auto &folder : folders) { From 6043f49bf133da474c1934cc6faca166600a03cf Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 16 Nov 2026 10:42:03 +0100 Subject: [PATCH 2/4] Wait briefly for listings at quit, and trim the listing pool The pool is still never deleted, so a listing hung on a dead mount can't stop Folio quitting, but a post routine now gives one that is just finishing a second before the statics it uses (QMimeDatabase) are destroyed. Once its model is gone a listing stops at the next entry, so that second covers everything but a hung read. The pool takes Qt's default thread count instead of a fixed 8, the trade-off comment the post routine replaces is gone, and listingPool()'s declaration moves out of the middle of list()'s comment. Co-Authored-By: Claude Opus 5.5 --- src/backend.cpp | 2 +- src/foldermodel.cpp | 17 +++++------------ src/foldermodel.h | 7 ++++--- tests/folio_tests.cpp | 7 ++----- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/backend.cpp b/src/backend.cpp index ef16ffa6..a3cc8bf9 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -396,7 +396,7 @@ void Backend::describe(const QVariantMap &item, bool inTrash) { }); // Stepping through files faster than they're read queues readings; the // ones overtaken before they start are skipped rather than left to tie - // up the global pool the folder counts and previews share. + // up the global pool's threads. watcher->setFuture(QtConcurrent::run([path, kind, inTrash, latest = m_describing, generation] { QVariantMap read; if (*latest != generation) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 8ff58ee8..051cd7ce 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -40,20 +41,12 @@ FolderModel::~FolderModel() { *m_latest = 0; } -// Folder listings run on a pool of their own, apart from the folder counts -// and previews on the global one: slow or huge subfolders being counted can't -// then hold up the listing of the folder just opened. -// -// Like Places::refreshDevices(), the pool is never deleted or waited on, so a -// listing hung on a dead mount can't hold up a quit. The trade: on exit a -// listing still running isn't waited for, as it would be on the global pool, -// and one that wakes during teardown can reach function-local statics (the -// QMimeDatabase in kindForMime()) already destroyed. +// Never deleted, like Places', so a listing hung on a dead mount can't stop +// Folio quitting; one just finishing gets a second before statics go. QThreadPool &FolderModel::listingPool() { static QThreadPool *const pool = [] { - auto *p = new QThreadPool; - p->setMaxThreadCount(8); - return p; + qAddPostRoutine([] { listingPool().waitForDone(1000); }); + return new QThreadPool; }(); return *pool; } diff --git a/src/foldermodel.h b/src/foldermodel.h index 39c46029..b20fb8b6 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -111,14 +111,15 @@ class FolderModel : public QAbstractListModel { Q_INVOKABLE bool isSelected(int row) const; Q_INVOKABLE QStringList selectedPaths() const; + // Listings run here, not on the global pool, so folder counts and file + // transfers can't hold up the folder just opened. + static QThreadPool &listingPool(); + // Synchronous building blocks, used by the background passes and tests. // A listing stops, empty, as soon as `stop` returns true. Names sort // by `locale`'s alphabet; a background pass passes the one it was // started with, because the default locale may only be read on the // thread that sets it. - // The pool folder listings run on (also reachable so tests can constrain it). - static QThreadPool &listingPool(); - static QVector list(const QString &path, bool showHidden, const QString &sort, QString *error, const std::function &stop = {}, const QLocale &locale = QLocale()); static Entry entryFor(const QFileInfo &info, const QMimeDatabase &mimes); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 23ca5bd7..734d81ff 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -745,11 +745,8 @@ private slots: void folderModelStopsReadingWhatsBeenOvertaken() { - // A big folder left before it's been read, or a search of it, stops - // being read instead of holding on to a thread of the listing pool - // that the next listing waits for. With a listing pool of one - // thread, the next folder waits for the overtaken - // read to stop, so it shows in a fraction of the time the read takes. + // With one listing thread, the next folder shows in a fraction of the + // time a big read takes only if the overtaken read stops. QTemporaryDir big, small, other; for (int i = 0; i < 20000; ++i) write(big.filePath(QString("%1.txt").arg(i)), "x"); From b247370dc6476d100e0c22f001bd8ef0c9d3fb23 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 16 Nov 2026 12:12:47 +0100 Subject: [PATCH 3/4] Keep the listing pool at 8 threads Without a set count the pool sizes itself to the CPU count, so on a one-CPU machine a single listing stuck in a filesystem call on a dead mount holds the only thread and every later listing, local folders included, waits behind it; changing folders can't interrupt a thread inside a syscall. The fixed 8 is what lets browsing survive a few stuck mounts. The test now also holds seven listing threads, and fails at a pool sized to fewer than 8 CPUs. Co-Authored-By: Claude Opus 5.5 --- src/foldermodel.cpp | 5 ++++- tests/folio_tests.cpp | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 051cd7ce..9b15cc3a 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -46,7 +46,10 @@ FolderModel::~FolderModel() { QThreadPool &FolderModel::listingPool() { static QThreadPool *const pool = [] { qAddPostRoutine([] { listingPool().waitForDone(1000); }); - return new QThreadPool; + auto *p = new QThreadPool; + // Eight, not the CPU count, so a few folders stuck on dead mounts leave others listing. + p->setMaxThreadCount(8); + return p; }(); return *pool; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 734d81ff..b0ae35f2 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -3317,20 +3317,27 @@ private slots: QCOMPARE(backend.takeSelection(), QString("report.pdf")); } - void folderModelListsWhileTheGlobalPoolIsHeld() + void folderModelListsWhileOtherWorkIsStuck() { + // Every thread of the global pool busy, and seven listings hung on dead mounts. QTemporaryDir dir; write(dir.filePath("a.txt"), "x"); QThreadPool *pool = QThreadPool::globalInstance(); pool->waitForDone(); - const int threads = pool->maxThreadCount(); std::atomic hold = true; - for (int i = 0; i < threads; ++i) - pool->start([&] { - while (hold) - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - }); - const auto release = qScopeGuard([&] { hold = false; pool->waitForDone(); }); + const auto stuck = [&] { + while (hold) + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + }; + for (int i = 0; i < pool->maxThreadCount(); ++i) + pool->start(stuck); + for (int i = 0; i < 7; ++i) + FolderModel::listingPool().start(stuck); + const auto release = qScopeGuard([&] { + hold = false; + pool->waitForDone(); + FolderModel::listingPool().waitForDone(); + }); FolderModel model; QSignalSpy loaded(&model, &FolderModel::loaded); model.setPath(dir.path()); From 0cfff7031a38e62a6d693a159950f5bed42fde15 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 16 Nov 2026 12:19:45 +0100 Subject: [PATCH 4/4] Say the listing pool's quit wait is a grace period, not a guarantee A listing stuck past the one-second wait can still reach statics such as kindForMime()'s QMimeDatabase once they are destroyed; the comment said only that one just finishing gets a second, losing the caveat the author had written down. The stuck-work test also drains the listing pool first, so a leftover listing can't take the slot it relies on. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Codex Medium --- src/foldermodel.cpp | 4 ++-- tests/folio_tests.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 9b15cc3a..4fbf50ad 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -41,8 +41,8 @@ FolderModel::~FolderModel() { *m_latest = 0; } -// Never deleted, like Places', so a listing hung on a dead mount can't stop -// Folio quitting; one just finishing gets a second before statics go. +// Never deleted, like Places', so a dead mount can't stop Folio quitting: a listing +// gets a second to finish, and one stuck longer may still meet destroyed statics. QThreadPool &FolderModel::listingPool() { static QThreadPool *const pool = [] { qAddPostRoutine([] { listingPool().waitForDone(1000); }); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index b0ae35f2..64219594 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -3324,6 +3324,7 @@ private slots: write(dir.filePath("a.txt"), "x"); QThreadPool *pool = QThreadPool::globalInstance(); pool->waitForDone(); + FolderModel::listingPool().waitForDone(); std::atomic hold = true; const auto stuck = [&] { while (hold)