Skip to content

Commit

Permalink
made resource closure synchronous to prevent bugs that can occur from…
Browse files Browse the repository at this point in the history
… out of order closing
  • Loading branch information
brennanjl committed Aug 21, 2023
1 parent accea1f commit b577871
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions internal/app/kwild/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net"
"path/filepath"
"sync"
"time"

// kwil-db
Expand Down Expand Up @@ -130,28 +129,14 @@ func (c *closeFuncs) addCloser(f func() error) {
c.closers = append(c.closers, f)
}

// closeAll concurrently closes all closers
// closeAll closeps all closers, in the order they were added
func (c *closeFuncs) closeAll() error {
errs := make([]error, 0)
errCh := make(chan error, len(c.closers))
wg := sync.WaitGroup{}

for _, f := range c.closers {
wg.Add(1)
go func(f func() error) {
err := f()
if err != nil {
errCh <- err
}
wg.Done()
}(f)
}

wg.Wait()
close(errCh)

for err := range errCh {
errs = append(errs, err)
for _, closer := range c.closers {
err := closer()
if err != nil {
errs = append(errs, err)
}
}

return errors.Join(errs...)
Expand Down

0 comments on commit b577871

Please sign in to comment.