fix goroutine leaks in repo commands
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Nov 21, 2017 at 15:39 UTC
2997f48a670536395f1072a2c80a54a8cd86884f
1 file changed
+29
-6
core/commands/repo.go
+29
-6
@@ -85,10 +85,18 @@ order to reclaim hard disk space.
85
errs := false
86
for res := range gcOutChan {
87
if res.Error != nil {
88
- outChan <- &GcResult{Error: res.Error.Error()}
88
+ select {
89
+ case outChan <- &GcResult{Error: res.Error.Error()}:
90
+ case <-req.Context().Done():
91
+ return
92
+ }
93
errs = true
94
} else {
91
- outChan <- &GcResult{Key: res.KeyRemoved}
95
+ select {
96
+ case outChan <- &GcResult{Key: res.KeyRemoved}:
97
+ case <-req.Context().Done():
98
+ return
99
+ }
100
}
101
}
102
if errs {
@@ -96,7 +104,10 @@ order to reclaim hard disk space.
104
}
105
} else {
106
err := corerepo.CollectResult(req.Context(), gcOutChan, func(k *cid.Cid) {
99
- outChan <- &GcResult{Key: k}
107
+ select {
108
+ case outChan <- &GcResult{Key: k}:
109
+ case <-req.Context().Done():
110
+ }
111
})
112
if err != nil {
113
res.SetError(err, cmdkit.ErrNormal)
@@ -291,17 +302,29 @@ var repoVerifyCmd = &oldcmds.Command{
302
for k := range keys {
303
_, err := bs.Get(k)
304
if err != nil {
294
- out <- &VerifyProgress{
305
+ select {
306
+ case out <- &VerifyProgress{
307
Msg: fmt.Sprintf("block %s was corrupt (%s)", k, err),
308
+ }:
309
+ case <-req.Context().Done():
310
+ return
311
}
312
fails++
313
}
314
i++
300
- out <- &VerifyProgress{Progress: i}
315
+ select {
316
+ case out <- &VerifyProgress{Progress: i}:
317
+ case <-req.Context().Done():
318
+ return
319
+ }
320
}
321
322
if fails == 0 {
304
- out <- &VerifyProgress{Msg: "verify complete, all blocks validated."}
323
+ select {
324
+ case out <- &VerifyProgress{Msg: "verify complete, all blocks validated."}:
325
+ case <-req.Context().Done():
326
+ return
327
+ }
328
} else {
329
res.SetError(fmt.Errorf("verify complete, some blocks were corrupt"), cmdkit.ErrNormal)
330
}