From bbef8780fd72c803273984dd250c0e45fa584c7a Mon Sep 17 00:00:00 2001 From: ihmc3jn09hk Date: Sat, 6 Jun 2020 12:59:54 +0800 Subject: [PATCH] Fix a bug in ListenerManager::getIOLoop() (#461) Co-authored-by: antao --- lib/src/ListenerManager.cc | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/src/ListenerManager.cc b/lib/src/ListenerManager.cc index fd01d851..4768b623 100644 --- a/lib/src/ListenerManager.cc +++ b/lib/src/ListenerManager.cc @@ -203,24 +203,22 @@ ListenerManager::~ListenerManager() trantor::EventLoop *ListenerManager::getIOLoop(size_t id) const { -#ifdef __linux__ - if (id >= listeningloopThreads_.size()) + auto const n = listeningloopThreads_.size(); + if (0 == n) { - LOG_TRACE << "Loop id (" << id << ") out of range [0-" - << listeningloopThreads_.size() << ")."; - id %= listeningloopThreads_.size(); + LOG_WARN << "Please call getIOLoop() after drogon::app().run()"; + return nullptr; + } + if (id >= n) + { + LOG_TRACE << "Loop id (" << id << ") out of range [0-" << n << ")."; + id %= n; LOG_TRACE << "Rounded to : " << id; } +#ifdef __linux__ assert(listeningloopThreads_[id]); return listeningloopThreads_[id]->getLoop(); #else - if (id >= ioLoopThreadPoolPtr_->size()) - { - LOG_TRACE << "Loop id (" << id << ") out of range [0-" - << listeningloopThreads_.size() << ")."; - id %= ioLoopThreadPoolPtr_->size(); - LOG_TRACE << "Rounded to : " << id; - } return ioLoopThreadPoolPtr_->getLoop(id); #endif }