From 8ba52a120f7a837b909c68e9094a2cca5d3302c0 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:48:56 +0300 Subject: [PATCH 1/2] Stop discarded thumbnail helpers on main --- src/thumbnails.cpp | 60 ++++++++++++++++++++++++++++--------------- src/thumbnails.h | 11 ++++---- tests/folio_tests.cpp | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 25 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index c0ae8b78..341421fb 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,24 @@ namespace { +bool isCancelled(const std::atomic_bool *cancelled) { + return cancelled && *cancelled; +} + +// Keep the existing timeout, but release a worker when its image is discarded. +bool waitForHelper(QProcess &process, const std::atomic_bool *cancelled) { + const QDeadlineTimer deadline(15000); + while (!isCancelled(cancelled) && !deadline.hasExpired()) { + if (process.waitForFinished(int(qMin(100, deadline.remainingTime())))) + return !isCancelled(cancelled); + if (process.state() == QProcess::NotRunning) + return false; + } + process.kill(); + process.waitForFinished(1000); + return false; +} + QString fileUri(const QString &path) { return QUrl::fromLocalFile(path).toString(QUrl::FullyEncoded); } @@ -162,7 +181,7 @@ QString Thumbnails::sharpPath(const QString &path, int pixels, bool alpha) const return QFileInfo(m_cache).absolutePath() + "/folio/sharp/" + QString::number(pixels) + "/" + key + (alpha ? ".png" : ".jpg"); } -QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *read) const { +QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *read, const std::atomic_bool *cancelled) const { *fresh = false; const QFileInfo info(path); if (!info.isFile()) @@ -178,7 +197,7 @@ QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, b QImageReader reader(path); reader.setAutoTransform(true); if (!reader.canRead()) - return load(path, mime, 1024); + return load(path, mime, 1024, cancelled); const QSize full = reader.size(); // Small enough already: read as it is, quickly, and not kept. const bool small = full.isValid() && full.width() <= pixels && full.height() <= pixels; @@ -186,7 +205,7 @@ QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, b reader.setScaledSize(full.scaled(pixels, pixels, Qt::KeepAspectRatio)); const QImage image = reader.read(); if (image.isNull()) - return load(path, mime, 1024); + return load(path, mime, 1024, cancelled); *fresh = !small; return image; } @@ -211,7 +230,9 @@ void Thumbnails::keep(const QImage &image, const QString &path, int pixels, cons } } -QImage Thumbnails::load(const QString &path, const QString &mime, int size) const { +QImage Thumbnails::load(const QString &path, const QString &mime, int size, const std::atomic_bool *cancelled) const { + if (isCancelled(cancelled)) + return {}; const QFileInfo info(path); if (!info.isFile()) return {}; @@ -235,7 +256,10 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons const QString type = FolderModel::typeByContent(path, named); const int pixels = bucketPixels(bucketFor(size)); bool attempted = false; - QImage image = generate(path, type, pixels, attempted); + QImage image = generate(path, type, pixels, attempted, cancelled); + // Discarding a picture says nothing about whether its file is broken. + if (isCancelled(cancelled)) + return {}; if (image.isNull()) { // Only a reader or thumbnailer that ran and failed is remembered, so // one installed later still gets its turn. @@ -249,7 +273,9 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons return image; } -QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const { +QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted, const std::atomic_bool *cancelled) const { + if (isCancelled(cancelled)) + return {}; // Common photo formats decode fastest in-process; anything Qt can't read // (HEIC, JPEG XL) falls through to the installed thumbnailers. if (mime.startsWith("image/")) { @@ -268,13 +294,13 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels const QString command = m_thumbnailers.value(mime); if (!command.isEmpty()) { attempted = true; - return runThumbnailer(command, path, pixels); + return runThumbnailer(command, path, pixels, cancelled); } // Without a PDF thumbnailer installed, Poppler's pdftoppm (there with // CUPS) draws the first page. if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) { attempted = true; - return pdfPage(path, pixels); + return pdfPage(path, pixels, cancelled); } return {}; } @@ -326,24 +352,21 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper return page; } -QImage Thumbnails::pdfPage(const QString &path, int pixels) { +QImage Thumbnails::pdfPage(const QString &path, int pixels, const std::atomic_bool *cancelled) { QTemporaryDir scratch; if (!scratch.isValid()) return {}; QProcess process; process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), path, scratch.filePath("page")}); - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); + if (!waitForHelper(process, cancelled)) return {}; - } if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) return {}; return QImage(scratch.filePath("page.png")); } -QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels) const { +QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, const std::atomic_bool *cancelled) const { QTemporaryDir scratch; if (!scratch.isValid()) return {}; @@ -372,11 +395,8 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i process.setProcessChannelMode(QProcess::ForwardedErrorChannel); process.start(program, args); // A broken file must never hang a tile forever. - if (!process.waitForFinished(15000)) { - process.kill(); - process.waitForFinished(1000); + if (!waitForHelper(process, cancelled)) return {}; - } if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) return {}; return QImage(output); @@ -475,7 +495,7 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable { const QString path = QUrl::fromPercentEncoding(parts.mid(4).join('/').toUtf8()); bool fresh = false; QDateTime modified; - m_image = m_thumbnails->sharp(path, mime, pixels, &fresh, &modified); + m_image = m_thumbnails->sharp(path, mime, pixels, &fresh, &modified, &m_cancelled); // Shown first, kept after: writing it shouldn't hold it up. Qt // may delete the response once it's finished, so nothing of it // is used after. @@ -497,7 +517,7 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable { const int size = parts.at(0).toInt(); const QString mime = QUrl::fromPercentEncoding(parts.at(2).toUtf8()); const QString path = QUrl::fromPercentEncoding(parts.mid(3).join('/').toUtf8()); - m_image = m_thumbnails->load(path, mime, qBound(64, size, 1024)); + m_image = m_thumbnails->load(path, mime, qBound(64, size, 1024), &m_cancelled); } if (m_image.isNull()) m_error = QStringLiteral("No thumbnail"); diff --git a/src/thumbnails.h b/src/thumbnails.h index 531e9c8d..326a793b 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -24,7 +25,7 @@ class Thumbnails { // Returns a thumbnail no larger than `size` on its longest side, from the // cache when it is fresh, or a null image when none can be made. - QImage load(const QString &path, const QString &mime, int size) const; + QImage load(const QString &path, const QString &mime, int size, const std::atomic_bool *cancelled = nullptr) const; // A picture at `pixels` on its longest side, as sharp as the details // column draws it: read from the file when it's that small already, @@ -34,7 +35,7 @@ class Thumbnails { // now, for keep() to cache, dated `modified`: the picture's time as it // was before its pixels were read, so one saved over meanwhile is made // again rather than kept old under its new time. - QImage sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *modified) const; + QImage sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *modified, const std::atomic_bool *cancelled = nullptr) const; void keep(const QImage &image, const QString &path, int pixels, const QDateTime &modified) const; static QString cacheKey(const QString &path); @@ -49,9 +50,9 @@ class Thumbnails { QString failPath(const QString &path) const; QString sharpPath(const QString &path, int pixels, bool alpha) const; // Sets `attempted` when Qt's reader or a thumbnailer could take the file. - QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const; - QImage runThumbnailer(const QString &command, const QString &path, int pixels) const; - static QImage pdfPage(const QString &path, int pixels); + QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted, const std::atomic_bool *cancelled) const; + QImage runThumbnailer(const QString &command, const QString &path, int pixels, const std::atomic_bool *cancelled) const; + static QImage pdfPage(const QString &path, int pixels, const std::atomic_bool *cancelled); void remember(QImage image, const QString &path, int size, const QDateTime &modified) const; void rememberFailure(const QString &path, const QDateTime &modified) const; bool failedBefore(const QString &path, const QDateTime &modified) const; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..58d0f028 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6549,6 +6549,58 @@ private slots: QCOMPARE(shownWidth(second.addMSecs(600)), 128); } + void thumbnailsStopWorkWhenItsImagesGoAway() + { + QTemporaryDir dir; + QImage frame(64, 32, QImage::Format_RGB32); + frame.fill(Qt::green); + QVERIFY(frame.save(dir.filePath("frame.png"))); + // One slow thumbnail per worker, like leaving a folder whose visible tiles are still loading. + write(dir.filePath("render"), "#!/bin/sh\nprintf started > \"$1.started\"\n" + "case \"$1\" in *slow*) exec /usr/bin/sleep 2.5;; esac\ncp \"" + + dir.filePath("frame.png").toUtf8() + "\" \"$2\"\n"); + write(dir.filePath("test.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/sh \"" + + dir.filePath("render").toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + QStringList files; + { + QQmlEngine engine; + engine.addImageProvider("thumb", new ThumbnailProvider(&thumbnails)); + QQmlComponent component(&engine); + component.setData("import QtQuick\nImage { asynchronous: true; cache: false }", QUrl()); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + auto source = [](const QString &path) { + return QUrl("image://thumb/128/0/video%2Fmp4/" + QUrl::toPercentEncoding(path)); + }; + QList images; + const auto clear = qScopeGuard([&] { qDeleteAll(images); }); + const int workers = qBound(2, QThread::idealThreadCount() / 2, 6); + for (int i = 0; i < workers; ++i) { + const QString path = dir.filePath("slow" + QString::number(i) + ".mp4"); + files << path; + write(path, "clip"); + images << component.createWithInitialProperties({{"source", source(path)}}); + QVERIFY(images.last()); + } + QTRY_VERIFY_WITH_TIMEOUT(std::all_of(files.begin(), files.end(), [](const QString &path) { + return QFile::exists(path + ".started"); + }), 3000); + + for (auto *image : images) + QVERIFY(image->setProperty("source", QUrl())); + const QString next = dir.filePath("next.mp4"); + write(next, "clip"); + QVERIFY(images.first()->setProperty("source", source(next))); + // Its image is ready without waiting for the discarded folder's 2.5-second jobs. + QTRY_COMPARE_WITH_TIMEOUT(images.first()->property("status").toInt(), 1, 1000); + QCOMPARE(images.first()->property("implicitWidth").toInt(), 64); + } + for (const auto &path : files) { + QVERIFY(!QFile::exists(dir.filePath("cache/normal/" + Thumbnails::cacheKey(path)))); + QVERIFY(!QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(path)))); + } + } + void thumbnailsReadInstalledThumbnailers() { QTemporaryDir dir; From 0d0e0f0dbe7b4930a8bbd8530c7d7b61940b616b Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:50:57 +0300 Subject: [PATCH 2/2] Match slow test helpers by filename --- tests/folio_tests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 58d0f028..7f6a9351 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6557,7 +6557,7 @@ private slots: QVERIFY(frame.save(dir.filePath("frame.png"))); // One slow thumbnail per worker, like leaving a folder whose visible tiles are still loading. write(dir.filePath("render"), "#!/bin/sh\nprintf started > \"$1.started\"\n" - "case \"$1\" in *slow*) exec /usr/bin/sleep 2.5;; esac\ncp \"" + "case \"${1##*/}\" in slow*.mp4) exec /usr/bin/sleep 2.5;; esac\ncp \"" + dir.filePath("frame.png").toUtf8() + "\" \"$2\"\n"); write(dir.filePath("test.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/sh \"" + dir.filePath("render").toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); @@ -6574,6 +6574,7 @@ private slots: }; QList images; const auto clear = qScopeGuard([&] { qDeleteAll(images); }); + // Fill every slot in ThumbnailProvider's bounded pool. const int workers = qBound(2, QThread::idealThreadCount() / 2, 6); for (int i = 0; i < workers; ++i) { const QString path = dir.filePath("slow" + QString::number(i) + ".mp4");