connectionmngr: prevent deadlock/crash
https://review.jami.net/c/dhtnet/+/26809 revisited
Change-Id: Ib8e20e6322fd06ca0a2acb25823549e3271ca593
diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp
index f425ef2..37becf7 100644
--- a/src/connectionmanager.cpp
+++ b/src/connectionmanager.cpp
@@ -749,14 +749,12 @@
info->tls_->setOnReady(
[w = weak_from_this(), dinfo, winfo=std::weak_ptr(info), deviceId = std::move(deviceId), vid = std::move(vid), name = std::move(name)](
bool ok) {
- if (auto shared = w.lock()) {
+ if (auto shared = w.lock())
if (auto info = winfo.lock()) {
shared->onTlsNegotiationDone(dinfo.lock(), info, ok, deviceId, vid, name);
- // Make sure there's another reference to info to avoid destruction when
- // we leave this scope (would lead to a deadlock on cbMtx_).
+ // Make another reference to info to avoid destruction (could lead to a deadlock/crash).
dht::ThreadPool::io().run([info = std::move(info)] {});
}
- }
});
return true;
}
@@ -1355,7 +1353,11 @@
info->tls_->setOnReady(
[w = weak_from_this(), dinfo, winfo=std::weak_ptr(info), deviceId = std::move(deviceId), vid = std::move(req.id)](bool ok) {
if (auto shared = w.lock())
- shared->onTlsNegotiationDone(dinfo.lock(), winfo.lock(), ok, deviceId, vid);
+ if (auto info = winfo.lock()) {
+ shared->onTlsNegotiationDone(dinfo.lock(), winfo.lock(), ok, deviceId, vid);
+ // Make another reference to info to avoid destruction (could lead to a deadlock/crash).
+ dht::ThreadPool::io().run([info = std::move(info)] {});
+ }
});
return true;
}