From 91fd4b6525ef151667c0816c4c3a318ccf6077af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 29 Aug 2023 20:50:01 -0400
Subject: [PATCH] upnp: stop asio context out of the loop

Change-Id: Iaee6527de1a599d6c79ef494637fb968a3035bbb
---
 src/upnp/upnp_context.cpp | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/upnp/upnp_context.cpp b/src/upnp/upnp_context.cpp
index 09f441e..8b80293 100644
--- a/src/upnp/upnp_context.cpp
+++ b/src/upnp/upnp_context.cpp
@@ -45,9 +45,9 @@ constexpr static uint16_t UPNP_UDP_PORT_MAX {UPNP_UDP_PORT_MIN + 5000};
 
 UPnPContext::UPnPContext(const std::shared_ptr<asio::io_context>& ioContext, const std::shared_ptr<dht::log::Logger>& logger)
  : ctx(createIoContext(ioContext, logger))
+ , logger_(logger)
  , mappingListUpdateTimer_(*ctx)
  , connectivityChangedTimer_(*ctx)
- , logger_(logger)
 {
     if (logger_) logger_->debug("Creating UPnPContext instance [{}]", fmt::ptr(this));
 
@@ -88,22 +88,13 @@ UPnPContext::shutdown(std::condition_variable& cv)
         proto->terminate();
     }
 
-    {
-        std::lock_guard<std::mutex> lock(mappingMutex_);
-        mappingList_->clear();
-        mappingListUpdateTimer_.cancel();
-        controllerList_.clear();
-        protocolList_.clear();
-        shutdownComplete_ = true;
-        cv.notify_one();
-    }
-
-    if (ioContextRunner_) {
-        if (logger_) logger_->debug("UPnPContext: stopping io_context thread");
-        ctx->stop();
-        ioContextRunner_->join();
-        ioContextRunner_.reset();
-    }
+    std::lock_guard<std::mutex> lock(mappingMutex_);
+    mappingList_->clear();
+    mappingListUpdateTimer_.cancel();
+    controllerList_.clear();
+    protocolList_.clear();
+    shutdownComplete_ = true;
+    cv.notify_one();
 }
 
 void
@@ -121,6 +112,13 @@ UPnPContext::shutdown()
     } else {
         if (logger_) logger_->error("Shutdown timed-out");
     }
+
+    if (ioContextRunner_) {
+        if (logger_) logger_->debug("UPnPContext: stopping io_context thread");
+        ctx->stop();
+        ioContextRunner_->join();
+        ioContextRunner_.reset();
+    }
 }
 
 UPnPContext::~UPnPContext()
-- 
GitLab