From f6baf4b80cd0c2ff88dde6e726fd8abf0c8f5c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Wed, 3 Jan 2024 15:51:36 -0500 Subject: [PATCH] natpmp: avoid to post after context shutdown Else, a deadlock may occurs https://git.jami.net/savoirfairelinux/jami-daemon/-/issues/936 Change-Id: Ib4a02845bd0b7e5d482f3fc5f5c8b8a01a50a80f --- src/upnp/protocol/natpmp/nat_pmp.cpp | 40 ++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/upnp/protocol/natpmp/nat_pmp.cpp b/src/upnp/protocol/natpmp/nat_pmp.cpp index 48e010a..7e88ad7 100644 --- a/src/upnp/protocol/natpmp/nat_pmp.cpp +++ b/src/upnp/protocol/natpmp/nat_pmp.cpp @@ -676,7 +676,13 @@ NatPmp::processIgdUpdate(UpnpIgdEvent event) if (observer_ == nullptr) return; // Process the response on the context thread. - ioContext->post([obs = observer_, igd = igd_, event] { obs->onIgdUpdated(igd, event); }); + ioContext->post([w = weak(), event] { + if (auto shared = w.lock()) { + if (!shared->shutdownComplete_) { + shared->observer_->onIgdUpdated(shared->igd_, event); + } + } + }); } void @@ -686,7 +692,13 @@ NatPmp::processMappingAdded(const Mapping& map) return; // Process the response on the context thread. - ioContext->post([obs = observer_, igd = igd_, map] { obs->onMappingAdded(igd, map); }); + ioContext->post([w=weak(), map] { + if (auto shared = w.lock()) { + if (!shared->shutdownComplete_) { + shared->observer_->onMappingAdded(shared->igd_, map); + } + } + }); } void @@ -696,7 +708,13 @@ NatPmp::processMappingRequestFailed(const Mapping& map) return; // Process the response on the context thread. - ioContext->post([obs = observer_, igd = igd_, map] { obs->onMappingRequestFailed(map); }); + ioContext->post([w=weak(), map] { + if (auto shared = w.lock()) { + if (!shared->shutdownComplete_) { + shared->observer_->onMappingRequestFailed(map); + } + } + }); } void @@ -706,7 +724,13 @@ NatPmp::processMappingRenewed(const Mapping& map) return; // Process the response on the context thread. - ioContext->post([obs = observer_, igd = igd_, map] { obs->onMappingRenewed(igd, map); }); + ioContext->post([w=weak(), map] { + if (auto shared = w.lock()) { + if (!shared->shutdownComplete_) { + shared->observer_->onMappingRenewed(shared->igd_, map); + } + } + }); } void @@ -716,7 +740,13 @@ NatPmp::processMappingRemoved(const Mapping& map) return; // Process the response on the context thread. - ioContext->post([obs = observer_, igd = igd_, map] { obs->onMappingRemoved(igd, map); }); + ioContext->post([w=weak(), map] { + if (auto shared = w.lock()) { + if (!shared->shutdownComplete_) { + shared->observer_->onMappingRemoved(shared->igd_, map); + } + } + }); } } // namespace upnp -- GitLab