fix(pin): goroutine leaks
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Sep 13, 2018 at 13:53 UTC
81453fec3ad630a0413b51d1727bf8bab363ee98
1 file changed
+37
-9
pin/gc/gc.go
+37
-9
@@ -58,7 +58,10 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
58
59
gcs, err := ColoredSet(ctx, pn, ds, bestEffortRoots, output)
60
if err != nil {
61
- output <- Result{Error: err}
61
+ select {
62
+ case output <- Result{Error: err}:
63
+ case <-ctx.Done():
64
+ }
65
return
66
}
67
emark.Append(logging.LoggableMap{
@@ -69,7 +72,10 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
72
73
keychan, err := bs.AllKeysChan(ctx)
74
if err != nil {
72
- output <- Result{Error: err}
75
+ select {
76
+ case output <- Result{Error: err}:
77
+ case <-ctx.Done():
78
+ }
79
return
80
}
81
@@ -108,7 +114,11 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
114
})
115
esweep.Done()
116
if errors {
111
- output <- Result{Error: ErrCannotDeleteSomeBlocks}
117
+ select {
118
+ case output <- Result{Error: ErrCannotDeleteSomeBlocks}:
119
+ case <-ctx.Done():
120
+ return
121
+ }
122
}
123
124
defer log.EventBegin(ctx, "GC.datastore").Done()
@@ -119,7 +129,10 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
129
130
err = gds.CollectGarbage()
131
if err != nil {
122
- output <- Result{Error: err}
132
+ select {
133
+ case output <- Result{Error: err}:
134
+ case <-ctx.Done():
135
+ }
136
return
137
}
138
}()
@@ -177,28 +190,40 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffo
190
links, err := ipld.GetLinks(ctx, ng, cid)
191
if err != nil {
192
errors = true
180
- output <- Result{Error: &CannotFetchLinksError{cid, err}}
193
+ select {
194
+ case output <- Result{Error: &CannotFetchLinksError{cid, err}}:
195
+ case <-ctx.Done():
196
+ }
197
}
198
return links, nil
199
}
200
err := Descendants(ctx, getLinks, gcs, pn.RecursiveKeys())
201
if err != nil {
202
errors = true
187
- output <- Result{Error: err}
203
+ select {
204
+ case output <- Result{Error: err}:
205
+ case <-ctx.Done():
206
+ }
207
}
208
209
bestEffortGetLinks := func(ctx context.Context, cid cid.Cid) ([]*ipld.Link, error) {
210
links, err := ipld.GetLinks(ctx, ng, cid)
211
if err != nil && err != ipld.ErrNotFound {
212
errors = true
194
- output <- Result{Error: &CannotFetchLinksError{cid, err}}
213
+ select {
214
+ case output <- Result{Error: &CannotFetchLinksError{cid, err}}:
215
+ case <-ctx.Done():
216
+ }
217
}
218
return links, nil
219
}
220
err = Descendants(ctx, bestEffortGetLinks, gcs, bestEffortRoots)
221
if err != nil {
222
errors = true
201
- output <- Result{Error: err}
223
+ select {
224
+ case output <- Result{Error: err}:
225
+ case <-ctx.Done():
226
+ }
227
}
228
229
for _, k := range pn.DirectKeys() {
@@ -208,7 +233,10 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffo
233
err = Descendants(ctx, getLinks, gcs, pn.InternalPins())
234
if err != nil {
235
errors = true
211
- output <- Result{Error: err}
236
+ select {
237
+ case output <- Result{Error: err}:
238
+ case <-ctx.Done():
239
+ }
240
}
241
242
if errors {