Commit 64329ed1 authored by Jeromy's avatar Jeromy

make sure not to orphan any extra goroutines

parent 5fb913d6
...@@ -368,14 +368,19 @@ func (bs *bitswap) wantNewBlocks(ctx context.Context, bkeys []u.Key) { ...@@ -368,14 +368,19 @@ func (bs *bitswap) wantNewBlocks(ctx context.Context, bkeys []u.Key) {
for i, k := range bkeys { for i, k := range bkeys {
message.AddEntry(k, kMaxPriority-i) message.AddEntry(k, kMaxPriority-i)
} }
wg := sync.WaitGroup{}
for _, p := range bs.engine.Peers() { for _, p := range bs.engine.Peers() {
wg.Add(1)
go func(p peer.ID) { go func(p peer.ID) {
defer wg.Done()
err := bs.send(ctx, p, message) err := bs.send(ctx, p, message)
if err != nil { if err != nil {
log.Debugf("Error sending message: %s", err) log.Debugf("Error sending message: %s", err)
} }
}(p) }(p)
} }
wg.Wait()
} }
func (bs *bitswap) ReceiveError(err error) { func (bs *bitswap) ReceiveError(err error) {
......
...@@ -90,7 +90,11 @@ func (bs *bitswap) clientWorker(parent context.Context) { ...@@ -90,7 +90,11 @@ func (bs *bitswap) clientWorker(parent context.Context) {
bs.wantlist.Add(k, kMaxPriority-i) bs.wantlist.Add(k, kMaxPriority-i)
} }
done := make(chan struct{})
go func() {
bs.wantNewBlocks(req.ctx, keys) bs.wantNewBlocks(req.ctx, keys)
close(done)
}()
// NB: Optimization. Assumes that providers of key[0] are likely to // NB: Optimization. Assumes that providers of key[0] are likely to
// be able to provide for all keys. This currently holds true in most // be able to provide for all keys. This currently holds true in most
...@@ -101,6 +105,10 @@ func (bs *bitswap) clientWorker(parent context.Context) { ...@@ -101,6 +105,10 @@ func (bs *bitswap) clientWorker(parent context.Context) {
if err != nil { if err != nil {
log.Debugf("error sending wantlist: %s", err) log.Debugf("error sending wantlist: %s", err)
} }
// Wait for wantNewBlocks to finish
<-done
case <-parent.Done(): case <-parent.Done():
return return
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment