Skip to content
Permalink
Browse files

Fix race condition and potential segfault in MythDownloadManager.

Put a lock around the creation of the MythDownloadManager to make
sure we don't get two created by a potential race condition.

Put a lock around access to the QNetworkDiskCache when called
via MythDownloadManager::GetLastModified().

Fixes #9231 using ideas from Jonatan from comhem.se, but most of
the patch (and potential issues) are mine.
  • Loading branch information
cpinkham committed Oct 19, 2011
1 parent 3049eb7 commit e7b14849adf4f97cda5798eb668bf457776fa7b5
Showing with 25 additions and 12 deletions.
  1. +25 −12 mythtv/libs/libmythbase/mythdownloadmanager.cpp
@@ -27,6 +27,7 @@ using namespace std;
#define LOC QString("DownloadManager: ")

MythDownloadManager *downloadManager = NULL;
QMutex dmCreateLock;

/*!
* \class MythDownloadInfo
@@ -134,21 +135,30 @@ void ShutdownMythDownloadManager(void)
*/
MythDownloadManager *GetMythDownloadManager(void)
{
if (!downloadManager)
{
downloadManager = new MythDownloadManager();
downloadManager->start();
while (!downloadManager->getQueueThread())
usleep(10000);
if (downloadManager)
return downloadManager;

QMutexLocker locker(&dmCreateLock);

downloadManager->moveToThread(downloadManager->getQueueThread());
downloadManager->setRunThread();
// Check once more in case the download manager was created
// while we were securing the lock.
if (downloadManager)
return downloadManager;

while (!downloadManager->isRunning())
usleep(10000);
MythDownloadManager *tmpDLM = new MythDownloadManager();
tmpDLM->start();
while (!tmpDLM->getQueueThread())
usleep(10000);

atexit(ShutdownMythDownloadManager);
}
tmpDLM->moveToThread(tmpDLM->getQueueThread());
tmpDLM->setRunThread();

while (!tmpDLM->isRunning())
usleep(10000);

downloadManager = tmpDLM;

atexit(ShutdownMythDownloadManager);

return downloadManager;
}
@@ -1039,7 +1049,10 @@ QDateTime MythDownloadManager::GetLastModified(const QString &url)
QDateTime result;

QDateTime now = QDateTime::currentDateTime();

m_infoLock->lock();
QNetworkCacheMetaData urlData = m_manager->cache()->metaData(QUrl(url));
m_infoLock->unlock();

if (urlData.isValid())
{

0 comments on commit e7b1484

Please sign in to comment.
You can’t perform that action at this time.