Fix improper handling of polling thread termination

This commit is contained in:
Cameron Gutman 2018-06-28 03:29:22 -07:00
parent 3d7c8d4bfb
commit 4c75456aee
2 changed files with 14 additions and 22 deletions

View File

@ -213,6 +213,10 @@ void ComputerManager::deleteHost(NvComputer* computer)
// We must wait here because we're going to delete computer // We must wait here because we're going to delete computer
// and we can't do that out from underneath the poller. // and we can't do that out from underneath the poller.
pollingThread->wait(); pollingThread->wait();
// The thread is safe to delete now
Q_ASSERT(pollingThread->isFinished());
delete pollingThread;
} }
m_PollThreads.remove(computer->uuid); m_PollThreads.remove(computer->uuid);
@ -232,8 +236,16 @@ void ComputerManager::stopPollingAsync()
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
// The threads will delete themselves when they terminate QThread* thread = i.value();
// Let this thread delete itself when it's finished
connect(thread, SIGNAL(finished()),
thread, SLOT(deleteLater()));
// The threads will delete themselves when they terminate,
// but we remove them from the polling threads map here.
i.value()->requestInterruption(); i.value()->requestInterruption();
i.remove();
} }
} }
@ -291,18 +303,6 @@ bool ComputerManager::addNewHost(QString address, bool mdns)
return true; return true;
} }
void
ComputerManager::handlePollThreadTermination(NvComputer* computer)
{
QWriteLocker lock(&m_Lock);
QThread* me = m_PollThreads[computer->uuid];
Q_ASSERT(me != nullptr);
m_PollThreads.remove(computer->uuid);
me->deleteLater();
}
void void
ComputerManager::handleComputerStateChanged(NvComputer* computer) ComputerManager::handleComputerStateChanged(NvComputer* computer)
{ {
@ -326,8 +326,6 @@ ComputerManager::startPollingComputer(NvComputer* computer)
} }
PcMonitorThread* thread = new PcMonitorThread(computer); PcMonitorThread* thread = new PcMonitorThread(computer);
connect(thread, SIGNAL(terminating(NvComputer*)),
this, SLOT(handlePollThreadTermination(NvComputer*)));
connect(thread, SIGNAL(computerStateChanged(NvComputer*)), connect(thread, SIGNAL(computerStateChanged(NvComputer*)),
this, SLOT(handleComputerStateChanged(NvComputer*))); this, SLOT(handleComputerStateChanged(NvComputer*)));
m_PollThreads[computer->uuid] = thread; m_PollThreads[computer->uuid] = thread;

View File

@ -156,7 +156,7 @@ private:
for (int i = 0; i < TRIES_BEFORE_OFFLINING; i++) { for (int i = 0; i < TRIES_BEFORE_OFFLINING; i++) {
for (auto& address : uniqueAddressList) { for (auto& address : uniqueAddressList) {
if (isInterruptionRequested()) { if (isInterruptionRequested()) {
goto Terminate; return;
} }
if (tryPollComputer(address, stateChanged)) { if (tryPollComputer(address, stateChanged)) {
@ -198,14 +198,10 @@ private:
// Wait a bit to poll again // Wait a bit to poll again
QThread::sleep(3); QThread::sleep(3);
} }
Terminate:
emit terminating(m_Computer);
} }
signals: signals:
void computerStateChanged(NvComputer* computer); void computerStateChanged(NvComputer* computer);
void terminating(NvComputer* computer);
private: private:
NvComputer* m_Computer; NvComputer* m_Computer;
@ -235,8 +231,6 @@ signals:
private slots: private slots:
void handleComputerStateChanged(NvComputer* computer); void handleComputerStateChanged(NvComputer* computer);
void handlePollThreadTermination(NvComputer* computer);
private: private:
void saveHosts(); void saveHosts();