From 106adf135cb4cc51c51673bb1f70766014a5c8d3 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 18 Oct 2022 14:29:17 +0800 Subject: [PATCH 1/2] Handle all types of alerts on shutdown There might be alerts already queued in buffer waiting to be handled at the time of pausing the session, so don't skip over them. --- src/base/bittorrent/sessionimpl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 9d85c7944..3f9d3633a 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2916,8 +2916,9 @@ void SessionImpl::saveResumeData() || (alertType == lt::save_resume_data_failed_alert::alert_type)) { hasWantedAlert = true; - handleAlert(a); } + + handleAlert(a); } if (hasWantedAlert) From 5f2d80786132eaf0569fa75e4937aafc57b3f302 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 23 Oct 2022 14:42:23 +0800 Subject: [PATCH 2/2] Ensure ongoing storage moving job will be completed when shutting down Discussion: https://github.com/qbittorrent/qBittorrent/pull/17885#issuecomment-1282467041 --- src/base/bittorrent/sessionimpl.cpp | 37 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 3f9d3633a..39f35bb2a 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2267,7 +2267,7 @@ bool SessionImpl::deleteTorrent(const TorrentID &id, const DeleteOption deleteOp { // Delete "move storage job" for the deleted torrent // (note: we shouldn't delete active job) - const auto iter = std::find_if(m_moveStorageQueue.begin() + 1, m_moveStorageQueue.end() + const auto iter = std::find_if((m_moveStorageQueue.begin() + 1), m_moveStorageQueue.end() , [torrent](const MoveStorageJob &job) { return job.torrentHandle == torrent->nativeHandle(); @@ -2899,21 +2899,40 @@ void SessionImpl::saveResumeData() ++m_numResumeData; } + // clear queued storage move jobs except the current ongoing one + if (m_moveStorageQueue.size() > 1) + { +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + m_moveStorageQueue = m_moveStorageQueue.mid(0, 1); +#else + m_moveStorageQueue.resize(1); +#endif + } + QElapsedTimer timer; timer.start(); - while (m_numResumeData > 0) + while ((m_numResumeData > 0) || !m_moveStorageQueue.isEmpty()) { const lt::seconds waitTime {5}; const lt::seconds expireTime {30}; + + // only terminate when no storage is moving + if (timer.hasExpired(lt::total_milliseconds(expireTime)) && m_moveStorageQueue.isEmpty()) + { + LogMsg(tr("Aborted saving resume data. Number of outstanding torrents: %1").arg(QString::number(m_numResumeData)) + , Log::CRITICAL); + break; + } + const std::vector alerts = getPendingAlerts(waitTime); bool hasWantedAlert = false; for (const lt::alert *a : alerts) { if (const int alertType = a->type(); - (alertType == lt::save_resume_data_alert::alert_type) - || (alertType == lt::save_resume_data_failed_alert::alert_type)) + (alertType == lt::save_resume_data_alert::alert_type) || (alertType == lt::save_resume_data_failed_alert::alert_type) + || (alertType == lt::storage_moved_alert::alert_type) || (alertType == lt::storage_moved_failed_alert::alert_type)) { hasWantedAlert = true; } @@ -2922,15 +2941,7 @@ void SessionImpl::saveResumeData() } if (hasWantedAlert) - { timer.start(); - } - else if (timer.hasExpired(lt::total_milliseconds(expireTime))) - { - LogMsg(tr("Aborted saving resume data. Number of outstanding torrents: %1").arg(QString::number(m_numResumeData)) - , Log::CRITICAL); - break; - } } } @@ -4658,7 +4669,7 @@ bool SessionImpl::addMoveTorrentStorageJob(TorrentImpl *torrent, const Path &new if (m_moveStorageQueue.size() > 1) { - auto iter = std::find_if(m_moveStorageQueue.begin() + 1, m_moveStorageQueue.end() + auto iter = std::find_if((m_moveStorageQueue.begin() + 1), m_moveStorageQueue.end() , [&torrentHandle](const MoveStorageJob &job) { return job.torrentHandle == torrentHandle;