Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

fix: limit use of custom context type #89

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func (bs *Bitswap) sendBlocks(ctx context.Context, env *engine.Envelope) {
}

func (bs *Bitswap) provideWorker(px process.Process) {
// FIXME: OnClosingContext returns a _custom_ context type.
// Unfortunately, deriving a new cancelable context from this custom
// type fires off a goroutine. To work around this, we create a single
// cancelable context up-front and derive all sub-contexts from that.
//
// See: https://github.com/ipfs/go-ipfs/issues/5810
ctx := procctx.OnClosingContext(px)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

limit := make(chan struct{}, provideWorkerMax)

Expand All @@ -108,7 +117,6 @@ func (bs *Bitswap) provideWorker(px process.Process) {
}()
ev := logging.LoggableMap{"ID": wid}

ctx := procctx.OnClosingContext(px) // derive ctx from px
defer log.EventBegin(ctx, "Bitswap.ProvideWorker.Work", ev, k).Done()

ctx, cancel := context.WithTimeout(ctx, provideTimeout) // timeout ctx
Expand All @@ -123,7 +131,7 @@ func (bs *Bitswap) provideWorker(px process.Process) {
// _ratelimited_ number of workers to handle each key.
for wid := 2; ; wid++ {
ev := logging.LoggableMap{"ID": 1}
log.Event(procctx.OnClosingContext(px), "Bitswap.ProvideWorker.Loop", ev)
log.Event(ctx, "Bitswap.ProvideWorker.Loop", ev)

select {
case <-px.Closing():
Expand Down