Skip to content

Commit e0f38fa

Browse files
Merge pull request #4406 from ipfs/fix/4405
Fix two race conditions (and possibly go routine leaks) in commands
2 parents 33c8207 + d89a4b6 commit e0f38fa

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

core/commands/add.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ You can now check what blocks have been created by:
342342
},
343343
PostRun: map[cmds.EncodingType]func(cmds.Request, cmds.ResponseEmitter) cmds.ResponseEmitter{
344344
cmds.CLI: func(req cmds.Request, re cmds.ResponseEmitter) cmds.ResponseEmitter {
345+
ctx := req.Context()
346+
345347
reNext, res := cmds.NewChanResponsePair(req)
346348
outChan := make(chan interface{})
347349

@@ -429,9 +431,6 @@ You can now check what blocks have been created by:
429431
bar.ShowBar = true
430432
bar.ShowTimeLeft = true
431433
}
432-
case <-req.Context().Done():
433-
re.SetError(req.Context().Err(), cmdkit.ErrNormal)
434-
return
435434
}
436435
}
437436
}
@@ -469,7 +468,12 @@ You can now check what blocks have been created by:
469468
return
470469
}
471470

472-
outChan <- v
471+
select {
472+
case outChan <- v:
473+
case <-ctx.Done():
474+
re.SetError(ctx.Err(), cmdkit.ErrNormal)
475+
return
476+
}
473477
}
474478
}()
475479

core/commands/pin.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,31 @@ var addPinCmd = &cmds.Command{
9090
v := new(dag.ProgressTracker)
9191
ctx := v.DeriveContext(req.Context())
9292

93-
ch := make(chan []*cid.Cid)
93+
type pinResult struct {
94+
pins []*cid.Cid
95+
err error
96+
}
97+
ch := make(chan pinResult, 1)
9498
go func() {
95-
defer close(ch)
9699
added, err := corerepo.Pin(n, ctx, req.Arguments(), recursive)
97-
if err != nil {
98-
res.SetError(err, cmdkit.ErrNormal)
99-
return
100-
}
101-
ch <- added
100+
ch <- pinResult{pins: added, err: err}
102101
}()
103102

104103
ticker := time.NewTicker(500 * time.Millisecond)
105104
defer ticker.Stop()
106105
defer close(out)
107106
for {
108107
select {
109-
case val, ok := <-ch:
110-
if !ok {
111-
// error already set just return
108+
case val := <-ch:
109+
if val.err != nil {
110+
res.SetError(val.err, cmdkit.ErrNormal)
112111
return
113112
}
114113

115114
if pv := v.Value(); pv != 0 {
116115
out <- &AddPinOutput{Progress: v.Value()}
117116
}
118-
out <- &AddPinOutput{Pins: cidsToStrings(val)}
117+
out <- &AddPinOutput{Pins: cidsToStrings(val.pins)}
119118
return
120119
case <-ticker.C:
121120
out <- &AddPinOutput{Progress: v.Value()}

0 commit comments

Comments
 (0)