Skip to content
Closed
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
23 changes: 23 additions & 0 deletions src/library/trackcollectionmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "library/trackcollectionmanager.h"

#include <QThreadPool>

#include "library/externaltrackcollection.h"
#include "library/scanner/libraryscanner.h"
#include "library/trackcollection.h"
Expand All @@ -17,6 +19,11 @@ const QString kConfigGroup = QStringLiteral("[TrackCollection]");

const ConfigKey kConfigKeyRepairDatabaseOnNextRestart(kConfigGroup, "RepairDatabaseOnNextRestart");

// Prevent infinite delays during shutdown. Long enough to finish
// the pending tasks and short enough to prevent users from killing
// the process.
constexpr int kThreadPoolTimeoutMillisOnShutdown = 30000; // 30 sec
Comment thread
Holzhaus marked this conversation as resolved.

inline
parented_ptr<TrackCollection> createInternalTrackCollection(
TrackCollectionManager* parent,
Expand Down Expand Up @@ -136,9 +143,23 @@ TrackCollectionManager::~TrackCollectionManager() {
kLogger.warning() << "BaseTrackCache is still in use";
}

// Ensure that all pending, concurrent tasks have been either
// cancelled or finished to prevent access to tracks in
// GlobalTrackCache before it gets deactivated.
kLogger.info()
<< "Stopping all pending tasks in worker threads";
QThreadPool::globalInstance()->clear();
if (!QThreadPool::globalInstance()->waitForDone(
kThreadPoolTimeoutMillisOnShutdown)) {
kLogger.warning()
<< "Failed to stop pending tasks in worker threads";
}

// Evict all remaining tracks from the cache to trigger
// updating of modified tracks. We assume that no other
// components are accessing those files at this point.
kLogger.info()
<< "Deactivating global track cache";
GlobalTrackCacheLocker().deactivateCache();

for (const auto& externalCollection : m_externalCollections) {
Expand All @@ -153,6 +174,8 @@ TrackCollectionManager::~TrackCollectionManager() {

m_pInternalCollection->disconnectDatabase();

kLogger.info()
<< "Destroying global track cache";
GlobalTrackCache::destroyInstance();
}

Expand Down