From 7737be35c5a51c8ae1e4605195d7267aa078d63e Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 18 Nov 2026 15:45:09 +0100 Subject: [PATCH] Stop a thumbnailer past its time with what it started A thumbnailer still running after 15 seconds was killed alone, so the programs it had started (glycin and the office thumbnailer start their own) ran on. Each now runs in a process group of its own, and past its time the whole group is stopped, as plugin commands already are. Co-Authored-By: Claude Opus 5.5 --- src/thumbnails.cpp | 31 ++++++++++++++++++------------- src/thumbnails.h | 8 +++++--- tests/folio_tests.cpp | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index b7a1e472..3d19e7f3 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include namespace { @@ -84,12 +86,16 @@ QStringList expandCommand(const QString &command, const QString &input, const QS // How a started helper ended. One that never started has said nothing about the file it was given. enum class Ran { NotStarted, Failed, Finished }; -Ran finish(QProcess &process) { - if (!process.waitForStarted(15000)) +Ran run(QProcess &process, const QString &program, const QStringList &args, int patience) { + // A group of its own, so what it starts (glycin and the office + // thumbnailer start their own) can be stopped with it. + process.setChildProcessModifier([] { ::setpgid(0, 0); }); + process.start(program, args); + if (!process.waitForStarted(patience)) return Ran::NotStarted; // A broken file must never hang a tile forever. - if (!process.waitForFinished(15000)) { - process.kill(); + if (!process.waitForFinished(patience)) { + ::kill(-pid_t(process.processId()), SIGKILL); process.waitForFinished(1000); return Ran::Failed; } @@ -99,11 +105,12 @@ Ran finish(QProcess &process) { } // namespace Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories, - const QString &remoteRoot) + const QString &remoteRoot, int patience) : m_cache(cacheDirectory.isEmpty() ? QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/thumbnails" : cacheDirectory), - m_remoteRoot(remoteRoot.isEmpty() ? Mounter::root() : remoteRoot) { + m_remoteRoot(remoteRoot.isEmpty() ? Mounter::root() : remoteRoot), + m_patience(patience) { QStringList directories = thumbnailerDirectories; if (directories.isEmpty()) { // Later directories win, so the user's own thumbnailers override the @@ -299,7 +306,7 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels // CUPS) draws the first page. if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) { QImage image; - attempted = pdfPage(path, pixels, image); + attempted = pdfPage(path, pixels, image, m_patience); return image; } return {}; @@ -352,14 +359,13 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper return page; } -bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image) { +bool Thumbnails::pdfPage(const QString &path, int pixels, QImage &image, int patience) { QTemporaryDir scratch; if (!scratch.isValid()) return false; QProcess process; - process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels), - path, scratch.filePath("page")}); - const Ran ran = finish(process); + const Ran ran = run(process, "pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", + QString::number(pixels), path, scratch.filePath("page")}, patience); if (ran == Ran::Finished) image = QImage(scratch.filePath("page.png")); return ran != Ran::NotStarted; @@ -392,8 +398,7 @@ bool Thumbnails::runThumbnailer(const QString &command, const QString &path, int QProcess process; process.setProcessChannelMode(QProcess::ForwardedErrorChannel); - process.start(program, args); - const Ran ran = finish(process); + const Ran ran = run(process, program, args, m_patience); if (ran == Ran::Finished) image = QImage(output); return ran != Ran::NotStarted; diff --git a/src/thumbnails.h b/src/thumbnails.h index 297e6c9f..062c0700 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -18,9 +18,10 @@ class Thumbnails { public: // `remoteRoot` is where GVfs mounts other machines (Mounter::root()); - // tests give their own. + // tests give their own. `patience` is how long a thumbnailer may take, + // in milliseconds; tests shorten it. explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {}, - const QString &remoteRoot = {}); + const QString &remoteRoot = {}, int patience = 15000); // 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. @@ -52,7 +53,7 @@ class Thumbnails { QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const; // Both answer whether the program ran at all: one that could not start has not judged the file. bool runThumbnailer(const QString &command, const QString &path, int pixels, QImage &image) const; - static bool pdfPage(const QString &path, int pixels, QImage &image); + static bool pdfPage(const QString &path, int pixels, QImage &image, int patience); 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; @@ -61,6 +62,7 @@ class Thumbnails { QString m_cache; QString m_remoteRoot; + int m_patience; QHash m_thumbnailers; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 53eb3076..6d0bb503 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6665,6 +6665,28 @@ private slots: QCOMPARE(QFileInfo(runs).size(), 4); } + void thumbnailsStopAThumbnailerWithWhatItStarted() + { + QTemporaryDir dir; + // The thumbnailer starts a helper of its own, notes the helper's number beside itself, and never ends. + const QString script = dir.filePath("hang.sh"), clip = dir.filePath("clip.mp4"); + write(script, "sleep 300 &\necho $! > \"$0.helper\"\nwait\n"); + write(dir.filePath("demo.thumbnailer"), + "[Thumbnailer Entry]\nExec=/bin/sh \"" + script.toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + write(clip, "not a video"); + const Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}, {}, 1000); + QVERIFY(thumbnails.load(clip, "video/mp4", 128).isNull()); + + // Past its time the helper is stopped as well. + QFile noted(script + ".helper"); + QVERIFY(noted.open(QIODevice::ReadOnly)); + const QString helper = QString::fromLatin1(noted.readAll()).trimmed(); + QVERIFY(helper.toInt() > 0); + QTRY_VERIFY(ended(helper.toInt())); + // A file its thumbnailer hangs on is held back like any other it fails on. + QVERIFY(QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(clip)))); + } + void thumbnailsRememberOnlyWhatFailed() { QTemporaryDir dir;