From 4a7a0fb477a376e5c281208525777c2a926f8c6a Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Thu, 6 Apr 2017 01:32:39 -0400 Subject: [PATCH] wsd: remove sockets when stopping poll thread And assume correct thread if poll thread is not running (i.e. no race). Change-Id: I17958e682aba434ebb47fe0de199b9f530b54dee Reviewed-on: https://gerrit.libreoffice.org/36183 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian --- net/Socket.hpp | 18 +++++++++++++++++- wsd/DocumentBroker.cpp | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/net/Socket.hpp b/net/Socket.hpp index d1a59b22c..cd1ea6cc4 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -264,7 +264,23 @@ public: /// Stop the polling thread. void stop() { + LOG_DBG("Stopping " << _name << " and removing all sockets."); _stop = true; + + assert(socket); + assertCorrectThread(); + + while (!_pollSockets.empty()) + { + const std::shared_ptr& socket = _pollSockets.back(); + + LOG_DBG("Removing socket #" << socket->getFD() << " from " << _name); + socket->assertCorrectThread(); + socket->setThreadOwner(std::thread::id(0)); + + _pollSockets.pop_back(); + } + wakeup(); } @@ -293,7 +309,7 @@ public: void assertCorrectThread() const { // 0 owner means detached and can be invoked by any thread. - const bool sameThread = (_owner == std::thread::id(0) || std::this_thread::get_id() == _owner); + const bool sameThread = (!isAlive() || _owner == std::thread::id(0) || std::this_thread::get_id() == _owner); if (!sameThread) LOG_ERR("Incorrect thread affinity for " << _name << ". Expected: 0x" << std::hex << _owner << " (" << std::dec << Util::getThreadId() << ") but called from 0x" << diff --git a/wsd/DocumentBroker.cpp b/wsd/DocumentBroker.cpp index 396bf52b4..3ab3d121e 100644 --- a/wsd/DocumentBroker.cpp +++ b/wsd/DocumentBroker.cpp @@ -264,6 +264,9 @@ void DocumentBroker::pollThread() _poll->poll(std::min(flushTimeoutMs - elapsedMs, POLL_TIMEOUT_MS / 5)); } + // Stop to mark it done and cleanup. + _poll->stop(); + // Async cleanup. LOOLWSD::doHousekeeping();