@cryptotaxi247 / kubo / commits / 8b99e60b8

bitswap/workers: fix proc / ctx wiring

This commit changes the order of the proc/ctx wiring, to ensure that the proc has been setup correctly before exiting. License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>

Juan Batiz-Benet committed Sep 5, 2015 at 04:37 UTC 8b99e60b8641fa9bd796cb082757bf07f59a65dd
1 file changed +10 -9
exchange/bitswap/bitswap.go
+10 -9
@@ -9,6 +9,7 @@ import (
9 "time"
10
11 process "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
12 + procctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14 blocks "github.com/ipfs/go-ipfs/blocks"
15 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
@@ -68,15 +69,6 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
69 return nil
70 })
71
71 - go func() {
72 - <-px.Closing() // process closes first
73 - cancelFunc()
74 - }()
75 - go func() {
76 - <-ctx.Done() // parent cancelled first
77 - px.Close()
78 - }()
79 -
72 bs := &Bitswap{
73 self: p,
74 blockstore: bstore,
@@ -94,6 +86,15 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
86
87 // Start up bitswaps async worker routines
88 bs.startWorkers(px, ctx)
89 +
90 + // bind the context and process.
91 + // do it over here to avoid closing before all setup is done.
92 + go func() {
93 + <-px.Closing() // process closes first
94 + cancelFunc()
95 + }()
96 + procctx.CloseAfterContext(px, ctx) // parent cancelled first
97 +
98 return bs
99 }
100