Skip to content

Commit

Permalink
C4Network2UPnP: Ensure all mappings are always removed even if the en…
Browse files Browse the repository at this point in the history
…gine is shutting down
  • Loading branch information
Fulgen301 committed Aug 17, 2024
1 parent 5f283f8 commit 3284520
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/C4Network2UPnPMiniUPnPc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ struct C4Network2UPnP::Impl
task = AddMappingInt(std::move(task), protocol, internalPort, externalPort);
}

static C4Task::OneShot ClearMappings(std::unique_ptr<Impl> self)
static C4Task::OneShot ClearMappings(std::unique_ptr<Impl> self, std::atomic_bool &done)
{
co_await C4Awaiter::ResumeInGlobalThreadPool();
co_await std::move(self->task);

done.store(true, std::memory_order_release);
done.notify_one();

// Doing a blocking wait here to ensure the coroutine
// is never suspended so that mapping removal succeeds
// even if the engine is shutting down.
std::move(self->task).Get();

if (!self->IsInitialized())
{
Expand Down Expand Up @@ -233,7 +240,9 @@ C4Network2UPnP::~C4Network2UPnP() noexcept
{
if (impl)
{
Impl::ClearMappings(std::move(impl));
std::atomic_bool done{false};
Impl::ClearMappings(std::move(impl), done);
done.wait(false, std::memory_order_acquire);
}
}

Expand Down

0 comments on commit 3284520

Please sign in to comment.