Skip to content

Commit 1580f7e

Browse files
committed
Fix CloseAll
1 parent 37009ea commit 1580f7e

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

closer/closer.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (c *closer) Add(f CloseFn) {
108108
}
109109

110110
for !c.funcs.CompareAndSwap(old, &funcs) {
111-
old := c.funcs.Load()
111+
old = c.funcs.Load()
112112
if old != nil {
113113
funcs = make([]CloseFn, len(*old)+1)
114114
copy(funcs, *old)
@@ -136,16 +136,19 @@ func (c *closer) CloseAll() {
136136
defer close(c.done)
137137

138138
ctx := context.WithoutCancel(*c.ctx.Load())
139-
funcs := *c.funcs.Swap(nil)
139+
funcs := c.funcs.Swap(nil)
140+
if funcs == nil {
141+
return
142+
}
140143

141-
errCh := make(chan error, len(funcs))
142-
for _, fn := range funcs {
144+
errCh := make(chan error, len(*funcs))
145+
for _, fn := range *funcs {
143146
go func(fn CloseFn) {
144147
errCh <- fn(ctx)
145148
}(fn)
146149
}
147150

148-
errs := make([]error, 0, len(funcs))
151+
errs := make([]error, 0, len(*funcs))
149152
for i := 0; i < cap(errs); i++ {
150153
if err := <-errCh; err != nil {
151154
errs = append(errs, err)

0 commit comments

Comments
 (0)