From 26ad6de3a4321badbdbbd7a2018a2e1497e7cc49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 8 Apr 2019 15:57:25 -0400
Subject: [PATCH] dhtrunner: avoid race condition when setting token

---
 src/dhtrunner.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index 6f15a2d9..86521bbe 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -665,17 +665,17 @@ DhtRunner::listen(InfoHash hash, ValueCallback vcb, Value::Filter f, Where w)
             listener.hash = hash;
             listener.f = std::move(f);
             listener.w = std::move(w);
-            listener.gcb = [hash,vcb,tokenbGlobal,this](const std::vector<Sp<Value>>& vals, bool expired){
+            listener.gcb = [hash,vcb,tokenbGlobal,this](const std::vector<Sp<Value>>& vals, bool expired) {
                 if (not vcb(vals, expired)) {
                     cancelListen(hash, tokenbGlobal);
                     return false;
                 }
                 return true;
             };
-            if (use_proxy)
-                listener.tokenProxyDht = dht.listen(hash, listener.gcb, listener.f, listener.w);
-            else
-                listener.tokenClassicDht = dht.listen(hash, listener.gcb, listener.f, listener.w);
+            if (auto token = dht.listen(hash, listener.gcb, listener.f, listener.w)) {
+                if (use_proxy)  listener.tokenProxyDht = token;
+                else            listener.tokenClassicDht = token;
+            }
             ret_token->set_value(tokenbGlobal);
 #else
             ret_token->set_value(dht.listen(hash, std::move(vcb), std::move(f), std::move(w)));
-- 
GitLab