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

Commit 6acf56a

Browse files
authored
Merge pull request #89 from ipfs/fix/worker-context
fix: limit use of custom context type
2 parents 5952645 + 799bfb3 commit 6acf56a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

workers.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ func (bs *Bitswap) sendBlocks(ctx context.Context, env *engine.Envelope) {
9898
}
9999

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

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

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

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

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

128136
select {
129137
case <-px.Closing():

0 commit comments

Comments
 (0)