From 2361f77fe4e902b806258de858721c9c88d16aaa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 15 Dec 2023 10:17:53 -0500
Subject: [PATCH] swarm_manager: move toConnectCb_ in ioPool

Else, it can cause a deadlock with connManagerMtx_

Change-Id: Idd9bb994a6c3f088b2998140a17fd41786d961a3
---
 src/jamidht/swarm/swarm_manager.cpp | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/jamidht/swarm/swarm_manager.cpp b/src/jamidht/swarm/swarm_manager.cpp
index 8d042ca3e2..1127db3b6b 100644
--- a/src/jamidht/swarm/swarm_manager.cpp
+++ b/src/jamidht/swarm/swarm_manager.cpp
@@ -55,15 +55,22 @@ SwarmManager::setKnownNodes(const std::vector<NodeId>& known_nodes)
             }
         }
     }
-    // If we detect a new node which already got a TCP link
-    // we can use it to speed-up the bootstrap (because opening
-    // a new channel will be easy)
-    std::set<NodeId> toConnect;
-    for (const auto& nodeId: newNodes) {
-        if (toConnectCb_ && toConnectCb_(nodeId))
-            toConnect.emplace(nodeId);
-    }
-    maintainBuckets(toConnect);
+
+    dht::ThreadPool::io().run([w=weak(), newNodes=std::move(newNodes)] {
+        auto shared = w.lock();
+        if (!shared)
+            return;
+        // If we detect a new node which already got a TCP link
+        // we can use it to speed-up the bootstrap (because opening
+        // a new channel will be easy)
+        std::set<NodeId> toConnect;
+        for (const auto& nodeId: newNodes) {
+            if (shared->toConnectCb_ && shared->toConnectCb_(nodeId))
+                toConnect.emplace(nodeId);
+        }
+        shared->maintainBuckets(toConnect);
+    });
+
 }
 
 void
-- 
GitLab