From 16ee52e3f882c3c20eb1e4a90ffc8e8f65fdad5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 1 Jun 2021 11:34:10 -0400
Subject: [PATCH] jamiaccount: set registration state in doUnregister

Change-Id: I9683e6eb8e3191c56a9fdb41c1e426e749f0349c
---
 contrib/src/opendht/SHA512SUMS          |  2 +-
 contrib/src/opendht/package.json        |  2 +-
 contrib/src/opendht/rules.mak           |  2 +-
 src/jamidht/archive_account_manager.cpp |  2 +-
 src/jamidht/jamiaccount.cpp             | 21 ++++++++++++++++-----
 5 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/contrib/src/opendht/SHA512SUMS b/contrib/src/opendht/SHA512SUMS
index 0629d8fd0b..c749f4bf0d 100644
--- a/contrib/src/opendht/SHA512SUMS
+++ b/contrib/src/opendht/SHA512SUMS
@@ -1 +1 @@
-692b098219f8da244ecc2fd89f215704bd0e097650fb6b1dced2400a52d4c53bc4bceaa15875772e8a4b4d55ccd6766c53c80417cd2bda3b42a32b74d1687981  opendht-14d882d22024310a7a2b1098b7dc39523f9fd592.tar.gz
\ No newline at end of file
+75d3da56bb9cb4ccc3afcb29b68a19f551f7a0f10193bf9a6efb2acaed2b4243c39daf96d8ab4e080596d44e15da1198a91cfaa7cd457ccdbb3d10a08f9b5fb0  opendht-2.2.0.tar.gz
\ No newline at end of file
diff --git a/contrib/src/opendht/package.json b/contrib/src/opendht/package.json
index 7fe6ce32d5..caaf490eaf 100644
--- a/contrib/src/opendht/package.json
+++ b/contrib/src/opendht/package.json
@@ -1,6 +1,6 @@
 {
     "name": "opendht",
-    "version": "14d882d22024310a7a2b1098b7dc39523f9fd592",
+    "version": "2.2.0",
     "url": "https://github.com/savoirfairelinux/opendht/archive/__VERSION__.tar.gz",
     "deps": [
         "argon2",
diff --git a/contrib/src/opendht/rules.mak b/contrib/src/opendht/rules.mak
index efffa6fe47..43e0c64fe1 100644
--- a/contrib/src/opendht/rules.mak
+++ b/contrib/src/opendht/rules.mak
@@ -1,5 +1,5 @@
 # OPENDHT
-OPENDHT_VERSION := 14d882d22024310a7a2b1098b7dc39523f9fd592
+OPENDHT_VERSION := 2.2.0
 OPENDHT_URL := https://github.com/savoirfairelinux/opendht/archive/$(OPENDHT_VERSION).tar.gz
 
 PKGS += opendht
diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp
index 661df63474..e9193d1bfc 100644
--- a/src/jamidht/archive_account_manager.cpp
+++ b/src/jamidht/archive_account_manager.cpp
@@ -758,7 +758,7 @@ ArchiveAccountManager::registerName(const std::string& password,
     try {
         auto archive = readArchive(password);
         auto privateKey = archive.id.first;
-        auto pk = privateKey->getPublicKey();
+        const auto& pk = privateKey->getPublicKey();
         publickey = pk.toString();
         accountId = pk.getId().toString();
         signedName = base64::encode(
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 20bf288864..c5a9cc9d8a 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -2487,11 +2487,17 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb)
         return;
     }
 
+    std::mutex mtx;
+    std::condition_variable cv;
+    bool shutdown_complete {false};
+
     JAMI_WARN("[Account %s] unregistering account %p", getAccountID().c_str(), this);
-    dht_->shutdown([this] {
+    dht_->shutdown([&] {
         JAMI_WARN("[Account %s] dht shutdown complete", getAccountID().c_str());
-        setRegistrationState(RegistrationState::UNREGISTERED);
-    });
+        std::lock_guard<std::mutex> lock(mtx);
+        shutdown_complete = true;
+        cv.notify_all();
+    }, false);
 
     {
         std::lock_guard<std::mutex> lk(pendingCallsMutex_);
@@ -2505,13 +2511,18 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb)
         shutdownConnections();
     }
 
-    dht_->join();
-
     // Release current upnp mapping if any.
     if (upnpCtrl_ and dhtUpnpMapping_.isValid()) {
         upnpCtrl_->releaseMapping(dhtUpnpMapping_);
     }
 
+    {
+        std::unique_lock<std::mutex> lock(mtx);
+        cv.wait(lock, [&]{ return shutdown_complete; });
+    }
+    dht_->join();
+    setRegistrationState(RegistrationState::UNREGISTERED);
+
     lock.unlock();
 
     if (released_cb)
-- 
GitLab