Skip to content

Commit

Permalink
Fix race condition and potential segfault in MythDownloadManager.
Browse files Browse the repository at this point in the history
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 e7b1484
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions mythtv/libs/libmythbase/mythdownloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace std;
#define LOC QString("DownloadManager: ")

MythDownloadManager *downloadManager = NULL;
QMutex dmCreateLock;

/*!
* \class MythDownloadInfo
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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())
{
Expand Down

0 comments on commit e7b1484

Please sign in to comment.