diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2013-01-10 23:11:04 -0800 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-14 11:35:17 +0100 |
commit | 60fc88a09c0127bf79563e5449e049f3c9e0fc55 (patch) | |
tree | e7212863b58e794c3e8ffbc2cf1483d30cf37c6d /tests/auto | |
parent | aeb2941ca1dcb901ca8bd6b74af456c1473728a4 (diff) |
Resolve some race conditions on tst_QSharedMemory
This test runs fine almost all of the time on systems with 1
processor, which were the norm when the test was written and are still
the way that the Qt Continuous Integration system works as of
today. But it falls flatly on multi-processor systems.
The root of the problem is that QSystemSemaphore recreates the
semaphore if it disappears underneath it. However, the recreation
process is not thread-safe at all: if two threads race to recreate it,
weird things might happen. strace on Linux shows that a thread got
stuck trying to acquire the semaphore:
<... nanosleep resumed> NULL) = 0
stat("/tmp/qipc_systemsem_market5c9f73af73334ffe350c60ec076e5744db0ecda3", {st_mode=S_IFREG|0640, st_size=0, ...}) = 0
stat("/tmp/qipc_systemsem_market5c9f73af73334ffe350c60ec076e5744db0ecda3", {st_mode=S_IFREG|0640, st_size=0, ...}) = 0
semget(0x51001388, 1, IPC_CREAT|IPC_EXCL|0600) = -1 EEXIST (File exists)
semget(0x51001388, 1, IPC_CREAT|0600) = 114786308
semop(114786308, {{0, -1, SEM_UNDO}}, 1 <unfinished ...>
This problem does not happen if the creation and destruction of the
QSharedMemory (which uses QSystemSemaphore) does not race with other
threads or processes attaching and detaching. For the threads test
it's easy. For the processes, we use stdin and stdout as a
communication channel.
Change-Id: Ie11b135431d4abfc59234654848b67f622eb03c9
Reviewed-by: Richard J. Moore <rich@kde.org>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qsharedmemory/sharedmemoryhelper/main.cpp | 9 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qsharedmemory/test/tst_qsharedmemory.cpp | 25 |
2 files changed, 24 insertions, 10 deletions
diff --git a/tests/auto/corelib/kernel/qsharedmemory/sharedmemoryhelper/main.cpp b/tests/auto/corelib/kernel/qsharedmemory/sharedmemoryhelper/main.cpp index 63f38f43f3..30c9d01e98 100644 --- a/tests/auto/corelib/kernel/qsharedmemory/sharedmemoryhelper/main.cpp +++ b/tests/auto/corelib/kernel/qsharedmemory/sharedmemoryhelper/main.cpp @@ -43,6 +43,7 @@ #include <QStringList> #include <QDebug> #include <QTest> +#include <stdio.h> void set(QSharedMemory &sm, int pos, QChar value) { @@ -82,7 +83,10 @@ int producer() return EXIT_FAILURE; } } + // tell parent we're ready //qDebug("producer created and attached"); + puts(""); + fflush(stdout); if (!producer.lock()) { qWarning() << "Could not lock" << producer.key(); @@ -129,9 +133,8 @@ int producer() //qDebug("producer done"); - // Sleep for a bit to let all consumers start, otherwise they will get stuck in the attach loop, - // because at least in Symbian the shared memory will be destroyed if there are no active handles to it. - QTest::qSleep(3000); + // Sleep for a bit to let all consumers exit + getchar(); return EXIT_SUCCESS; } diff --git a/tests/auto/corelib/kernel/qsharedmemory/test/tst_qsharedmemory.cpp b/tests/auto/corelib/kernel/qsharedmemory/test/tst_qsharedmemory.cpp index 5d9b470494..c7c75947ad 100644 --- a/tests/auto/corelib/kernel/qsharedmemory/test/tst_qsharedmemory.cpp +++ b/tests/auto/corelib/kernel/qsharedmemory/test/tst_qsharedmemory.cpp @@ -177,6 +177,7 @@ void tst_QSharedMemory::cleanup() #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> +#include <errno.h> #endif QString tst_QSharedMemory::helperBinary() @@ -211,7 +212,7 @@ int tst_QSharedMemory::remove(const QString &key) int id = shmget(unix_key, 0, 0660); if (-1 == id) { - qDebug() << "shmget failed"; + qDebug() << "shmget failed" << strerror(errno); return -4; } @@ -634,9 +635,8 @@ class Producer : public QThread { public: - void run() + Producer() : producer(QLatin1String("market")) { - QSharedMemory producer(QLatin1String("market")); int size = 1024; if (!producer.create(size)) { // left over from a crash... @@ -646,7 +646,11 @@ public: QVERIFY(producer.create(size)); } } - QVERIFY(producer.isAttached()); + } + + void run() + { + char *memory = (char*)producer.data(); memory[1] = '0'; QTime timer; @@ -671,6 +675,8 @@ public: QVERIFY(producer.unlock()); } + + QSharedMemory producer; private: }; @@ -701,6 +707,7 @@ void tst_QSharedMemory::simpleThreadedProducerConsumer() #endif Producer p; + QVERIFY(p.producer.isAttached()); if (producerIsThread) p.start(); @@ -741,9 +748,10 @@ void tst_QSharedMemory::simpleProcessProducerConsumer() rememberKey("market"); QProcess producer; - producer.setProcessChannelMode(QProcess::ForwardedChannels); producer.start(helperBinary(), QStringList("producer")); QVERIFY2(producer.waitForStarted(), "Could not start helper binary"); + QVERIFY2(producer.waitForReadyRead(), "Helper process failed to create shared memory segment: " + + producer.readAllStandardError()); QList<QProcess*> consumers; unsigned int failedProcesses = 0; @@ -758,8 +766,6 @@ void tst_QSharedMemory::simpleProcessProducerConsumer() ++failedProcesses; } - QVERIFY(producer.waitForFinished(5000)); - bool consumerFailed = false; while (!consumers.isEmpty()) { @@ -773,6 +779,11 @@ void tst_QSharedMemory::simpleProcessProducerConsumer() } QCOMPARE(consumerFailed, false); QCOMPARE(failedProcesses, (unsigned int)(0)); + + // tell the producer to exit now + producer.write("", 1); + producer.waitForBytesWritten(); + QVERIFY(producer.waitForFinished(5000)); } void tst_QSharedMemory::uniqueKey_data() |