fix(bitswap) pass derived context to called functions
@whyrusleeping @jbenet License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 26, 2014 at 12:58 UTC
829eac3012ab958914626d1963ced83a875d68f4
1 file changed
+8
-4
exchange/bitswap/bitswap.go
+8
-4
@@ -87,9 +87,13 @@ type bitswap struct {
87
// deadline enforced by the context.
88
func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) {
89
90
- // make sure to derive a new |ctx| and pass it to children. It's correct to
91
- // listen on |parent| here, but incorrect to pass |parent| to new async
92
- // functions. This is difficult to enforce. May this comment keep you safe.
90
+ // Any async work initiated by this function must end when this function
91
+ // returns. To ensure this, derive a new context. Note that it is okay to
92
+ // listen on parent in this scope, but NOT okay to pass |parent| to
93
+ // functions called by this one. Otherwise those functions won't return
94
+ // when this context Otherwise those functions won't return when this
95
+ // context's cancel func is executed. This is difficult to enforce. May
96
+ // this comment keep you safe.
97
98
ctx, cancelFunc := context.WithCancel(parent)
99
@@ -101,7 +105,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
105
log.Event(ctx, "GetBlockRequestEnd", &k)
106
}()
107
104
- promise, err := bs.GetBlocks(parent, []u.Key{k})
108
+ promise, err := bs.GetBlocks(ctx, []u.Key{k})
109
if err != nil {
110
return nil, err
111
}