query: fixed race condition
Juan Batiz-Benet committed
Mar 7, 2015 at 02:44 UTC
7a03d677ffc7a1e029f9b047422f6152f7447632
1 file changed
+15
routing/dht/query.go
+15
@@ -52,6 +52,12 @@ type queryFunc func(context.Context, peer.ID) (*dhtQueryResult, error)
52
53
// Run runs the query at hand. pass in a list of peers to use first.
54
func (q *dhtQuery) Run(ctx context.Context, peers []peer.ID) (*dhtQueryResult, error) {
55
+ select {
56
+ case <-ctx.Done():
57
+ return nil, ctx.Err()
58
+ default:
59
+ }
60
+
61
ctx, cancel := context.WithCancel(ctx)
62
defer cancel()
63
@@ -104,6 +110,15 @@ func (r *dhtQueryRunner) Run(peers []peer.ID) (*dhtQueryResult, error) {
110
r.addPeerToQuery(r.cg.Context(), p)
111
}
112
113
+ // may be closed already. this caused an odd race (where we attempt to
114
+ // add a child to an already closed ctxgroup). this is a temp workaround
115
+ // as we'll switch to using a proc here soon.
116
+ select {
117
+ case <-r.cg.Closed():
118
+ return nil, r.cg.Context().Err()
119
+ default:
120
+ }
121
+
122
// go do this thing.
123
// do it as a child func to make sure Run exits
124
// ONLY AFTER spawn workers has exited.