Commit 2be5de8f authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Jeromy

fix(bitswap) pass derived context to called functions

@whyrusleeping @jbenet

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 58ad863d
...@@ -87,9 +87,13 @@ type bitswap struct { ...@@ -87,9 +87,13 @@ type bitswap struct {
// deadline enforced by the context. // deadline enforced by the context.
func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) { func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) {
// make sure to derive a new |ctx| and pass it to children. It's correct to // Any async work initiated by this function must end when this function
// listen on |parent| here, but incorrect to pass |parent| to new async // returns. To ensure this, derive a new context. Note that it is okay to
// functions. This is difficult to enforce. May this comment keep you safe. // listen on parent in this scope, but NOT okay to pass |parent| to
// functions called by this one. Otherwise those functions won't return
// when this context Otherwise those functions won't return when this
// context's cancel func is executed. This is difficult to enforce. May
// this comment keep you safe.
ctx, cancelFunc := context.WithCancel(parent) ctx, cancelFunc := context.WithCancel(parent)
...@@ -101,7 +105,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err ...@@ -101,7 +105,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
log.Event(ctx, "GetBlockRequestEnd", &k) log.Event(ctx, "GetBlockRequestEnd", &k)
}() }()
promise, err := bs.GetBlocks(parent, []u.Key{k}) promise, err := bs.GetBlocks(ctx, []u.Key{k})
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
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