From 48b0cff08ee4c26d7925f492816b9431a7efc052 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <rostydela@gmail.com>
Date: Fri, 15 Jan 2016 14:54:56 -0500
Subject: [PATCH] pht: fix multiple DoneCallBack calls

---
 src/indexation/pht.cpp | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp
index 4ac390a5..f12abed9 100644
--- a/src/indexation/pht.cpp
+++ b/src/indexation/pht.cpp
@@ -19,9 +19,13 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
     auto mid = (*lo + *hi)/2;
     auto first_res = std::make_shared<node_lookup_result>();
     auto second_res = std::make_shared<node_lookup_result>();
-    auto on_done = [=](){
+    auto on_done = [=](bool ok){
         bool is_leaf = first_res->is_pht and not second_res->is_pht;
-        if (is_leaf or *lo > *hi) {
+        if (not ok) {
+            if (done_cb)
+                done_cb(false);
+        }
+        else if (is_leaf or *lo > *hi) {
             // leaf node
             if (cb)
                 if (vals->size() == 0 and max_common_prefix_len and mid > 0) {
@@ -33,10 +37,14 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
                 cb(*vals, p.getPrefix(mid));
             if (done_cb)
                 done_cb(true);
-        } else {
+        } else if (first_res->is_pht) {
             // internal node
             *lo = mid+1;
             lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len);
+        } else {
+            // first get failed before second.
+            if (done_cb)
+                done_cb(false);
         }
     };
     if (*lo <= *hi) {
@@ -79,8 +87,9 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
                 [=](bool ok) {
                     if (not ok) {
                         // DHT failed
-                        if (done_cb)
-                            done_cb(false);
+                        first_res->done = true;
+                        if (done_cb and second_res->done)
+                            on_done(false);
                     }
                     else {
                         if (not first_res->is_pht) {
@@ -90,7 +99,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
                         } else {
                             first_res->done = true;
                             if (second_res->done)
-                                on_done();
+                                on_done(true);
                         }
                     }
                 }, pht_filter);
@@ -100,18 +109,19 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi,
                     [=](bool ok) {
                         if (not ok) {
                             // DHT failed
-                            if (done_cb)
-                                done_cb(false);
+                            second_res->done = true;
+                            if (done_cb and first_res->done)
+                                on_done(false);
                         }
                         else {
                             second_res->done = true;
                             if (first_res->done)
-                                on_done();
+                                on_done(true);
                         }
                     }, pht_filter);
 
     } else {
-        on_done();
+        on_done(true);
     }
 }
 
-- 
GitLab