Skip to content

Commit

Permalink
fix: don't leak goroutines if there are no files to watch (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Nov 22, 2024
1 parent f208671 commit aebc27e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion configx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ func (p *Provider) createProviders(ctx context.Context) (providers []koanf.Provi
p.logger.WithField("files", paths).Debug("Adding config files.")

c := make(watcherx.EventChannel)
go p.watchForFileChanges(ctx, c)

defer func() {
if err == nil && len(paths) > 0 {
go p.watchForFileChanges(ctx, c)
}
}()
for _, path := range paths {
fp, err := NewKoanfFile(path)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions configx/provider_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func assertNoOpenFDs(t require.TestingT, dir, name string) {
}
var b, be bytes.Buffer
// we are only interested in the file descriptors, so we use the `-F f` option
c := exec.Command("lsof", "-n", "-F", "f", filepath.Join(dir, name))
c := exec.Command("lsof", "-n", "-F", "f", "--", filepath.Join(dir, name))
c.Stdout = &b
c.Stderr = &be
exitErr := new(exec.ExitError)
require.ErrorAs(t, c.Run(), &exitErr, "got stout: %s\nstderr: %s", b.String(), be.String())
require.ErrorAsf(t, c.Run(), &exitErr, "File %q has open file descriptor.\nGot stout: %s\nstderr: %s", filepath.Join(dir, name), b.String(), be.String())
assert.Equal(t, 1, exitErr.ExitCode(), "got stout: %s\nstderr: %s", b.String(), be.String())
}

Expand Down

0 comments on commit aebc27e

Please sign in to comment.