From b631e1e4f245dc8ba4460bda6acb4ed1ef63bcdc Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Thu, 11 Aug 2011 13:26:15 +1000 Subject: [PATCH] Fix race condition in processJobs() Don't modify list of running jobs when a job is aborted since the job may have just started. Wait till the next time processJobs() is invoked for a new job and discard the aborted job at that time. (cherry picked from commit f55ecc080d0c5eca4e65a235c63ab13867c86874 in 4.8) Task-number: QTBUG-20841 Change-Id: Icb4cd089505c0634d3fec023b52c61bbc878404f Reviewed-on: http://codereview.qt.nokia.com/2837 Reviewed-by: Bea Lam Reviewed-by: Qt Sanity Bot --- src/declarative/util/qdeclarativexmllistmodel.cpp | 29 +++++++------------- 1 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp index 7ac6c76..86e6e9e 100644 --- a/src/declarative/util/qdeclarativexmllistmodel.cpp +++ b/src/declarative/util/qdeclarativexmllistmodel.cpp @@ -289,11 +289,8 @@ int QDeclarativeXmlQueryEngine::doQuery(QString query, QString namespaces, QByte void QDeclarativeXmlQueryEngine::abort(int id) { QMutexLocker ml(&m_mutex); - if (id != -1) { + if (id != -1) m_cancelledJobs.insert(id); - if (m_threadObject) - m_threadObject->processJobs(); - } } void QDeclarativeXmlQueryEngine::run() @@ -314,25 +311,19 @@ void QDeclarativeXmlQueryEngine::processJobs() QMutexLocker locker(&m_mutex); while (true) { - if (m_cancelledJobs.isEmpty() && m_jobs.isEmpty()) + if (m_jobs.isEmpty()) return; - if (!m_cancelledJobs.isEmpty()) { - for (QList::Iterator it = m_jobs.begin(); it != m_jobs.end(); ++it) { - int queryId = (*it).queryId; - if (m_cancelledJobs.remove(queryId)) - it = m_jobs.erase(it); - } - m_cancelledJobs.clear(); + XmlQueryJob currentJob = m_jobs.takeLast(); + while (m_cancelledJobs.remove(currentJob.queryId)) { + if (m_jobs.isEmpty()) + return; + currentJob = m_jobs.takeLast(); } - if (!m_jobs.isEmpty()) { - XmlQueryJob currentJob = m_jobs.takeLast(); - - locker.unlock(); - processQuery(¤tJob); - locker.relock(); - } + locker.unlock(); + processQuery(¤tJob); + locker.relock(); } } -- 1.7.2.5