Skip to content

Commit a2e0bb5

Browse files
committed
reap child processes to prevent leaving them defunct
1 parent 3049d89 commit a2e0bb5

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

deck.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,17 @@ func (deck *Deck) backgroundForKey(dev *streamdeck.Device, key uint8) image.Imag
203203

204204
func (deck *Deck) WindowChanged(window ActiveWindow) {
205205
verboseLog("windowChanged %s:%s %s", window.resource, window.title, window.id)
206-
var match *WindowWidgets
206+
var match = false
207207
for _, w := range deck.windows {
208208
if w.Matches(window) {
209-
match = &w
209+
verboseLog("windowMatch: %s:%s", w.resource, w.title)
210+
for i, widget := range w.widgets {
211+
deck.overrideWidget(i, widget)
212+
}
213+
match = true
210214
}
211215
}
212-
if match != nil {
213-
for i, widget := range match.widgets {
214-
deck.overrideWidget(i, widget)
215-
}
216-
} else {
216+
if !match {
217217
for key := range deck.overrides {
218218
deck.restoreWidget(key)
219219
}
@@ -299,17 +299,12 @@ func executeCommand(cmd string) error {
299299
args := SPACES.Split(cmd, -1)
300300
exe := expandExecutable(args[0])
301301

302-
c := exec.Command(exe, args[1:]...) //nolint:gosec
303-
if *verboseConfig {
304-
c.Stdout = os.Stdout
305-
c.Stderr = os.Stderr
306-
}
307-
308-
if e := c.Start(); e != nil {
302+
command := exec.Command(exe, args[1:]...)
303+
if err := command.Start(); err != nil {
309304
errorLogF("failed to execute '%s %s'", exe, args[1:])
310-
return e
305+
return err
311306
}
312-
return c.Process.Release()
307+
return command.Process.Release()
313308
}
314309

315310
func (deck *Deck) Widgets(yield func(Widget) bool) {

main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ func expandPath(base, path string) (string, error) {
8686
return filepath.Abs(path)
8787
}
8888

89+
func reapChildProcesses() {
90+
sigs := make(chan os.Signal)
91+
signal.Notify(sigs, syscall.SIGCHLD)
92+
93+
for range sigs {
94+
for {
95+
var status syscall.WaitStatus
96+
pid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, nil)
97+
if pid <= 0 || err != nil {
98+
break
99+
}
100+
verboseLog("reaped pid=%d status=%d", pid, status.ExitStatus())
101+
}
102+
}
103+
}
104+
89105
func eventLoop(dev *streamdeck.Device, tch chan interface{}) error {
90106
sigs := make(chan os.Signal, 1)
91107
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
@@ -97,6 +113,7 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) error {
97113
keyTimestamps := make(map[uint8]time.Time)
98114

99115
go pa.Start()
116+
go reapChildProcesses()
100117

101118
wch := MonitorActiveWindowChanged()
102119

0 commit comments

Comments
 (0)