diff --git a/src/upnp/upnp_context.cpp b/src/upnp/upnp_context.cpp
index 09f441e1ab3f9539987b77df0cc744a9713851cf..8b8029367a6cdbf15c8195eda6da96993b531bb7 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()