From b709c8162c71a05a099273c60db01fba66a5010d Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 16 Dec 2015 14:24:52 -0800 Subject: [PATCH] Fix node flapping race --- assignment-client/src/octree/OctreeServer.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 937998f28a..04b6845ad8 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -879,10 +879,11 @@ OctreeServer::UniqueSendThread OctreeServer::createSendThread(const SharedNodePo } void OctreeServer::removeSendThread() { - auto sendThread = static_cast(sender()); - - // This deletes the unique_ptr, so sendThread is destructed after that line - _sendThreads.erase(sendThread->getNodeUuid()); + // If the object has been deleted since the event was queued, sender() will return nullptr + if (auto sendThread = qobject_cast(sender())) { + // This deletes the unique_ptr, so sendThread is destructed after that line + _sendThreads.erase(sendThread->getNodeUuid()); + } } void OctreeServer::handleOctreeQueryPacket(QSharedPointer message, SharedNodePointer senderNode) { @@ -893,7 +894,13 @@ void OctreeServer::handleOctreeQueryPacket(QSharedPointer messa nodeList->updateNodeWithDataFromPacket(message, senderNode); auto it = _sendThreads.find(senderNode->getUUID()); - if (it == _sendThreads.end() || it->second->isShuttingDown()) { + if (it == _sendThreads.end()) { + _sendThreads.emplace(senderNode->getUUID(), createSendThread(senderNode)); + } else if (it->second->isShuttingDown()) { + auto& sendThread = *it->second; + sendThread.setIsShuttingDown(); + _sendThreads.erase(it); // Remove right away and wait on thread to be + _sendThreads.emplace(senderNode->getUUID(), createSendThread(senderNode)); } } @@ -1224,7 +1231,6 @@ void OctreeServer::aboutToFinish() { // Shut down all the send threads for (auto& it : _sendThreads) { auto& sendThread = *it.second; - sendThread.disconnect(this); // Disconnect so that removeSendThread doesn't get called later sendThread.setIsShuttingDown(); }