From 7a03d677ffc7a1e029f9b047422f6152f7447632 Mon Sep 17 00:00:00 2001
From: Juan Batiz-Benet <juan@benet.ai>
Date: Sat, 7 Mar 2015 02:44:20 -0800
Subject: [PATCH] query: fixed race condition

---
 routing/dht/query.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/routing/dht/query.go b/routing/dht/query.go
index aacab106f..888fdd65d 100644
--- a/routing/dht/query.go
+++ b/routing/dht/query.go
@@ -52,6 +52,12 @@ type queryFunc func(context.Context, peer.ID) (*dhtQueryResult, error)
 
 // Run runs the query at hand. pass in a list of peers to use first.
 func (q *dhtQuery) Run(ctx context.Context, peers []peer.ID) (*dhtQueryResult, error) {
+	select {
+	case <-ctx.Done():
+		return nil, ctx.Err()
+	default:
+	}
+
 	ctx, cancel := context.WithCancel(ctx)
 	defer cancel()
 
@@ -104,6 +110,15 @@ func (r *dhtQueryRunner) Run(peers []peer.ID) (*dhtQueryResult, error) {
 		r.addPeerToQuery(r.cg.Context(), p)
 	}
 
+	// may be closed already. this caused an odd race (where we attempt to
+	// add a child to an already closed ctxgroup). this is a temp workaround
+	// as we'll switch to using a proc here soon.
+	select {
+	case <-r.cg.Closed():
+		return nil, r.cg.Context().Err()
+	default:
+	}
+
 	// go do this thing.
 	// do it as a child func to make sure Run exits
 	// ONLY AFTER spawn workers has exited.
-- 
GitLab