Skip to content

Commit eb7fd9f

Browse files
committed
upnp: ensure proper removal of open ports
Every port but one was being left open after quitting the app. This change ensures no deadlocks or race conditions prevent the closure of mapped ports. Change-Id: I953a2a714f0f4550d64cce66ac61ec93c4c9798f
1 parent d5466df commit eb7fd9f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/upnp/protocol/pupnp/pupnp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ PUPnP::requestMappingRemove(const Mapping& mapping)
648648
asio::post(*ioContext, [w = weak(), mapping] {
649649
if (auto upnpThis = w.lock()) {
650650
// Abort if we are shutting down.
651-
if (not upnpThis->isRunning())
651+
if (not upnpThis->clientRegistered_)
652652
return;
653653
if (upnpThis->actionDeletePortMapping(mapping)) {
654654
upnpThis->processRemoveMapAction(mapping);

src/upnp/upnp_context.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ UPnPContext::shutdown(std::condition_variable& cv)
9898

9999
stopUpnp(true);
100100

101+
std::promise<void> ioFlushed;
102+
asio::post(*ioCtx, [&ioFlushed] { ioFlushed.set_value(); });
103+
auto status = ioFlushed.get_future().wait_for(std::chrono::seconds(5));
104+
if (status == std::future_status::timeout) {
105+
if (logger_)
106+
logger_->warn("Timed out waiting for pending UPnP operations to complete");
107+
}
108+
101109
for (auto const& [_, proto] : protocolList_) {
102110
proto->terminate();
103111
}
@@ -224,8 +232,6 @@ UPnPContext::stopUpnp(bool forceRelease)
224232
toRemoveList.emplace_back(map);
225233
}
226234
}
227-
// Invalidate the current IGD.
228-
currentIgd_.reset();
229235
}
230236
for (auto const& map : toRemoveList) {
231237
requestRemoveMapping(map);
@@ -238,6 +244,11 @@ UPnPContext::stopUpnp(bool forceRelease)
238244
unregisterMapping(map);
239245
}
240246
}
247+
{
248+
// Invalidate the current IGD.
249+
std::lock_guard lock(mappingMutex_);
250+
currentIgd_.reset();
251+
}
241252

242253
// Clear all current IGDs.
243254
for (auto const& [_, protocol] : protocolList_) {

0 commit comments

Comments
 (0)