Skip to content

Commit 40ef87a

Browse files
committed
C4Network2UPnP: Ensure all mappings are always removed even if the engine is shutting down
1 parent 4b2cbcc commit 40ef87a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/C4Network2UPnPMiniUPnPc.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ struct C4Network2UPnP::Impl
7070
task = AddMappingInt(std::move(task), protocol, internalPort, externalPort);
7171
}
7272

73-
static C4Task::OneShot ClearMappings(std::unique_ptr<Impl> self)
73+
static C4Task::OneShot ClearMappings(std::unique_ptr<Impl> self, std::atomic_bool &done)
7474
{
7575
co_await C4Awaiter::ResumeInGlobalThreadPool();
76-
co_await std::move(self->task);
76+
77+
done.store(true, std::memory_order_release);
78+
done.notify_one();
79+
80+
// Doing a blocking wait here to ensure the coroutine
81+
// is never suspended so that mapping removal succeeds
82+
// even if the engine is shutting down.
83+
std::move(self->task).Get();
7784

7885
if (!self->IsInitialized())
7986
{
@@ -233,7 +240,9 @@ C4Network2UPnP::~C4Network2UPnP() noexcept
233240
{
234241
if (impl)
235242
{
236-
Impl::ClearMappings(std::move(impl));
243+
std::atomic_bool done{false};
244+
Impl::ClearMappings(std::move(impl), done);
245+
done.wait(false, std::memory_order_acquire);
237246
}
238247
}
239248

0 commit comments

Comments
 (0)