diff --git a/loolwsd/DocumentBroker.cpp b/loolwsd/DocumentBroker.cpp index 624b2be93..3adc1ad23 100644 --- a/loolwsd/DocumentBroker.cpp +++ b/loolwsd/DocumentBroker.cpp @@ -410,30 +410,30 @@ size_t DocumentBroker::addSession(std::shared_ptr& session) const auto id = session->getId(); const std::string aMessage = "session " + id + " " + _docKey + "\n"; - std::lock_guard lock(_mutex); - - // Request a new session from the child kit. - Log::debug("DocBroker to Child: " + aMessage.substr(0, aMessage.length() - 1)); - _childProcess->sendTextFrame(aMessage); - - auto ret = _sessions.emplace(id, session); - if (!ret.second) - { - Log::warn("DocumentBroker: Trying to add already existing session."); - } - - if (session->isReadOnly()) - { - Log::debug("Adding a readonly session [" + id + "]"); - } - - // Below values are recalculated when startDestroy() is called (before destroying the - // document). It is safe to reset their values to their defaults whenever a new session is added. - _lastEditableSession = false; - _markToDestroy = false; - try { + std::lock_guard lock(_mutex); + + // Request a new session from the child kit. + Log::debug("DocBroker to Child: " + aMessage.substr(0, aMessage.length() - 1)); + _childProcess->sendTextFrame(aMessage); + + auto ret = _sessions.emplace(id, session); + if (!ret.second) + { + Log::warn("DocumentBroker: Trying to add already existing session."); + } + + if (session->isReadOnly()) + { + Log::debug("Adding a readonly session [" + id + "]"); + } + + // Below values are recalculated when startDestroy() is called (before destroying the + // document). It is safe to reset their values to their defaults whenever a new session is added. + _lastEditableSession = false; + _markToDestroy = false; + load(id, std::to_string(_childProcess->getPid())); } catch (const StorageSpaceLowException&) @@ -491,6 +491,7 @@ size_t DocumentBroker::removeSession(const std::string& id) void DocumentBroker::alertAllUsersOfDocument(const std::string& cmd, const std::string& kind) { + Util::assertIsNotLocked(_mutex); std::lock_guard lock(_mutex); std::stringstream ss; diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp index a55c70e29..d99fd36f7 100644 --- a/loolwsd/Util.hpp +++ b/loolwsd/Util.hpp @@ -106,6 +106,13 @@ namespace Util assert(!mtx.try_lock()); } + inline + void assertIsNotLocked(std::mutex& mtx) + { + assert(mtx.try_lock()); + mtx.unlock(); + } + /// Safely remove a file or directory. /// Supresses exception when the file is already removed. /// This can happen when there is a race (unavoidable) or when