Skip to content

Commit

Permalink
fix(bitswap) pass derived context to called functions
Browse files Browse the repository at this point in the history
@whyrusleeping @jbenet

License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
  • Loading branch information
Brian Tiger Chow committed Nov 26, 2014
1 parent 11bb58e commit ada5714
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ type bitswap struct {
// deadline enforced by the context.
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
// listen on |parent| here, but incorrect to pass |parent| to new async
// functions. This is difficult to enforce. May this comment keep you safe.
// Any async work initiated by this function must end when this function
// returns. To ensure this, derive a new context. Note that it is okay to
// 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

This comment has been minimized.

Copy link
@whyrusleeping

whyrusleeping Nov 27, 2014

Member

typo here, looks like you copy pasted the beginning of the sentence twice.

This comment has been minimized.

Copy link
@btc

btc Nov 28, 2014

Contributor

thanks for catching this.

// context's cancel func is executed. This is difficult to enforce. May
// this comment keep you safe.

ctx, cancelFunc := context.WithCancel(parent)

Expand All @@ -101,7 +105,7 @@ func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
log.Event(ctx, "GetBlockRequestEnd", &k)
}()

promise, err := bs.GetBlocks(parent, []u.Key{k})
promise, err := bs.GetBlocks(ctx, []u.Key{k})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ada5714

Please # to comment.