From d5c6704475e2571a8d084f7ecf8bee3ea2c7a173 Mon Sep 17 00:00:00 2001 From: flow Date: Fri, 24 Feb 2023 20:34:43 -0300 Subject: [PATCH] fix: prevent crash when aborting import task while extracting pack Signed-off-by: flow --- launcher/InstanceImportTask.cpp | 20 +++++++++++--------- launcher/InstanceImportTask.h | 1 - 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/launcher/InstanceImportTask.cpp b/launcher/InstanceImportTask.cpp index 6b5317e5..080828a8 100644 --- a/launcher/InstanceImportTask.cpp +++ b/launcher/InstanceImportTask.cpp @@ -66,7 +66,12 @@ bool InstanceImportTask::abort() if (m_filesNetJob) m_filesNetJob->abort(); - m_extractFuture.cancel(); + if (m_extractFuture.isRunning()) { + // NOTE: The tasks created by QtConcurrent::run() can't actually get cancelled, + // but we can use this call to check the state when the extraction finishes. + m_extractFuture.cancel(); + m_extractFuture.waitForFinished(); + } return Task::abort(); } @@ -185,18 +190,20 @@ void InstanceImportTask::processZipPack() // make sure we extract just the pack m_extractFuture = QtConcurrent::run(QThreadPool::globalInstance(), MMCZip::extractSubDir, m_packZip.get(), root, extractDir.absolutePath()); connect(&m_extractFutureWatcher, &QFutureWatcher::finished, this, &InstanceImportTask::extractFinished); - connect(&m_extractFutureWatcher, &QFutureWatcher::canceled, this, &InstanceImportTask::extractAborted); m_extractFutureWatcher.setFuture(m_extractFuture); } void InstanceImportTask::extractFinished() { m_packZip.reset(); - if (!m_extractFuture.result()) - { + + if (m_extractFuture.isCanceled()) + return; + if (!m_extractFuture.result().has_value()) { emitFailed(tr("Failed to extract modpack")); return; } + QDir extractDir(m_stagingPath); qDebug() << "Fixing permissions for extracted pack files..."; @@ -250,11 +257,6 @@ void InstanceImportTask::extractFinished() } } -void InstanceImportTask::extractAborted() -{ - emitAborted(); -} - void InstanceImportTask::processFlame() { FlameCreationTask* inst_creation_task = nullptr; diff --git a/launcher/InstanceImportTask.h b/launcher/InstanceImportTask.h index 6b8ac966..7fda439f 100644 --- a/launcher/InstanceImportTask.h +++ b/launcher/InstanceImportTask.h @@ -81,7 +81,6 @@ private slots: void downloadProgressChanged(qint64 current, qint64 total); void downloadAborted(); void extractFinished(); - void extractAborted(); private: /* data */ NetJob::Ptr m_filesNetJob;