@cryptotaxi247 / kubo / commits / 8256b1805

make sure not to orphan any extra goroutines

Jeromy committed Feb 26, 2015 at 16:43 UTC 8256b180588bfd9a8286b9416b5aafb3624c10ec
2 files changed +14 -1
exchange/bitswap/bitswap.go
+5
@@ -368,14 +368,19 @@ func (bs *bitswap) wantNewBlocks(ctx context.Context, bkeys []u.Key) {
368 for i, k := range bkeys {
369 message.AddEntry(k, kMaxPriority-i)
370 }
371 +
372 + wg := sync.WaitGroup{}
373 for _, p := range bs.engine.Peers() {
374 + wg.Add(1)
375 go func(p peer.ID) {
376 + defer wg.Done()
377 err := bs.send(ctx, p, message)
378 if err != nil {
379 log.Debugf("Error sending message: %s", err)
380 }
381 }(p)
382 }
383 + wg.Wait()
384 }
385
386 func (bs *bitswap) ReceiveError(err error) {
exchange/bitswap/workers.go
+9 -1
@@ -90,7 +90,11 @@ func (bs *bitswap) clientWorker(parent context.Context) {
90 bs.wantlist.Add(k, kMaxPriority-i)
91 }
92
93 - bs.wantNewBlocks(req.ctx, keys)
93 + done := make(chan struct{})
94 + go func() {
95 + bs.wantNewBlocks(req.ctx, keys)
96 + close(done)
97 + }()
98
99 // NB: Optimization. Assumes that providers of key[0] are likely to
100 // be able to provide for all keys. This currently holds true in most
@@ -101,6 +105,10 @@ func (bs *bitswap) clientWorker(parent context.Context) {
105 if err != nil {
106 log.Debugf("error sending wantlist: %s", err)
107 }
108 +
109 + // Wait for wantNewBlocks to finish
110 + <-done
111 +
112 case <-parent.Done():
113 return
114 }