Skip to content

Commit

Permalink
fix: Don't leak signal notification resources (#504)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Duchon <[email protected]>
  • Loading branch information
rhansen and buchdag authored Mar 21, 2023
1 parent 62cd989 commit 63f8ea7
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func (g *generator) generateFromSignals() {
go func() {
defer g.wg.Done()

sigChan := newSignalChannel()
sigChan, cleanup := newSignalChannel()
defer cleanup()
for {
sig := <-sigChan
log.Printf("Received signal: %s\n", sig)
Expand Down Expand Up @@ -148,7 +149,8 @@ func (g *generator) generateAtInterval() {
go func(cfg config.Config) {
defer g.wg.Done()

sigChan := newSignalChannel()
sigChan, cleanup := newSignalChannel()
defer cleanup()
for {
select {
case <-ticker.C:
Expand Down Expand Up @@ -217,7 +219,8 @@ func (g *generator) generateFromEvents() {
go func() {
// channel will be closed by go-dockerclient
eventChan := make(chan *docker.APIEvents, 100)
sigChan := newSignalChannel()
sigChan, cleanup := newSignalChannel()
defer cleanup()

for {
watching := false
Expand Down Expand Up @@ -469,11 +472,10 @@ func (g *generator) getContainers() ([]*context.RuntimeContainer, error) {

}

func newSignalChannel() <-chan os.Signal {
func newSignalChannel() (<-chan os.Signal, func()) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)

return sig
return sig, func() { signal.Stop(sig) }
}

func newDebounceChannel(input chan *docker.APIEvents, wait *config.Wait) chan *docker.APIEvents {
Expand Down

0 comments on commit 63f8ea7

Please sign in to comment.