Skip to content

Commit 7075255

Browse files
committed
Remove direct os.Stdin access from daemon command
1 parent 35c4d78 commit 7075255

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

cli/cli_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func inspect(t *testing.T, fset *token.FileSet, node ast.Node) bool {
7373
wanted := map[string]bool{
7474
"os.Stdout": true,
7575
"os.Stderr": true,
76+
"os.Stdin": true,
7677
}
7778
name := expr(n)
7879
if wanted[name] {

cli/daemon/daemon.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package daemon
1818
import (
1919
"errors"
2020
"fmt"
21-
"io"
22-
"io/ioutil"
2321
"net"
2422
"os"
2523
"strings"
@@ -120,11 +118,7 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
120118

121119
if !daemonize {
122120
// When parent process ends terminate also the daemon
123-
go func() {
124-
// Stdin is closed when the controlling parent process ends
125-
_, _ = io.Copy(ioutil.Discard, os.Stdin)
126-
os.Exit(0)
127-
}()
121+
go feedback.ExitWhenParentProcessEnds()
128122
}
129123

130124
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", ip, port))

cli/feedback/terminal.go

+10
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ func InputUserField(prompt string, secret bool) (string, error) {
7272

7373
return string(value), nil
7474
}
75+
76+
// ExitWhenParentProcessEnds waits until the controlling parent process ends and then exits
77+
// the current process. This is useful to terminate the current process when it is daemonized
78+
// and the controlling parent process is terminated to avoid leaving zombie processes.
79+
// It is recommended to call this function as a goroutine.
80+
func ExitWhenParentProcessEnds() {
81+
// Stdin is closed when the controlling parent process ends
82+
_, _ = io.Copy(io.Discard, os.Stdin)
83+
os.Exit(0)
84+
}

0 commit comments

Comments
 (0)