From 463f26f417958d55270118f3aeac279ce689f2bc Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Wed, 5 Apr 2017 00:26:31 -0400 Subject: [PATCH] wsd: mark detached sockets to have no owner The DocBroker might not get a chance to take ownership of a socket (which is done via callbacks that are invoked in the poll loop) if it (or WSD) is flagged for termination. In that case, DocBroker doesn't take ownership but ultimately needs to disconnect the socket. By detaching ownership we signal that any thread can rightly take ownership and thus avoid spurious warning or assertions. Change-Id: Idb192bfaac05c5c86809cb21876f3926a080b775 Reviewed-on: https://gerrit.libreoffice.org/36117 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian --- net/Socket.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/net/Socket.hpp b/net/Socket.hpp index f0a9975c9..4fa51dce0 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -198,7 +198,8 @@ public: virtual bool isCorrectThread() { #if ENABLE_DEBUG - const bool sameThread = std::this_thread::get_id() == _owner; + // 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); if (!sameThread) LOG_WRN("#" << _fd << " Invoked from foreign thread. Expected: 0x" << std::hex << _owner << " but called from 0x" << std::this_thread::get_id() << " (" << @@ -294,12 +295,13 @@ public: /// Are we running in either shutdown, or the polling thread. bool isCorrectThread() const { - if (std::this_thread::get_id() != _owner) + // 0 owner means detached and can be invoked by any thread. + if (_owner != std::thread::id(0) && std::this_thread::get_id() != _owner) LOG_WRN("Incorrect thread affinity for " << _name << ". Expected: 0x" << std::hex << _owner << " (" << std::dec << Util::getThreadId() << ") but called from 0x" << std::hex << std::this_thread::get_id() << std::dec << ", stop: " << _stop); - return _stop || std::this_thread::get_id() == _owner; + return _stop || _owner == std::thread::id(0) || std::this_thread::get_id() == _owner; } /// Poll the sockets for available data to read or buffer to write. @@ -392,6 +394,7 @@ public: { LOG_DBG("Removing socket #" << _pollFds[i].fd << " (of " << _pollSockets.size() << ") from " << _name); + _pollSockets[i]->setThreadOwner(std::thread::id(0)); _pollSockets.erase(_pollSockets.begin() + i); } }