From 6714ee37ab2ad8bee58b05ce0367feebebf97f08 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Fri, 16 Nov 2018 13:08:35 -0500
Subject: [PATCH] account: fix deadlocks during (un)register

Change-Id: Ieb4821822ecfb1bacfee5ad4abec96f861b63950
---
 src/ringdht/ringaccount.cpp |  4 +++-
 src/sip/sipaccount.cpp      | 11 ++++++-----
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 7f9b230556..f764543885 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -2507,6 +2507,7 @@ RingAccount::doUnregister(std::function<void(bool)> released_cb)
 
     if (registrationState_ == RegistrationState::INITIALIZING
      || registrationState_ == RegistrationState::ERROR_NEED_MIGRATION) {
+        lock.unlock();
         if (released_cb) released_cb(false);
         return;
     }
@@ -2527,9 +2528,10 @@ RingAccount::doUnregister(std::function<void(bool)> released_cb)
     saveNodes(dht_.exportNodes());
     saveValues(dht_.exportValues());
     dht_.join();
-    setRegistrationState(RegistrationState::UNREGISTERED);
 
     lock.unlock();
+    setRegistrationState(RegistrationState::UNREGISTERED);
+
     if (released_cb)
         released_cb(false);
 }
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index de2139946c..0bc395865d 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -735,11 +735,12 @@ void SIPAccount::doRegister()
 
 void SIPAccount::doRegister1_()
 {
-    std::lock_guard<std::mutex> lock(configurationMutex_);
-
-    if (isIP2IP()) {
-        doRegister2_();
-        return;
+    {
+        std::lock_guard<std::mutex> lock(configurationMutex_);
+        if (isIP2IP()) {
+            doRegister2_();
+            return;
+        }
     }
 
     std::weak_ptr<SIPAccount> weak_acc = std::static_pointer_cast<SIPAccount>(shared_from_this());
-- 
GitLab