Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <QTemporaryDir>
#include <QUrl>
#include <atomic>
#include <csignal>
#include <unistd.h>

namespace {

Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/thumbnails.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -61,6 +62,7 @@ class Thumbnails {

QString m_cache;
QString m_remoteRoot;
int m_patience;
QHash<QString, QString> m_thumbnailers;
};

Expand Down
22 changes: 22 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading