@@ -92,9 +92,9 @@ func main() {
9292 log .Infof ("network tracer started" )
9393
9494 // Handles signals, which tells us whether we should exit.
95- e := make (chan bool )
96- handleSignals ( e )
97- <- e
95+ exit := make (chan bool )
96+ go HandleSignals ( exit )
97+ <- exit
9898}
9999
100100func gracefulExit () {
@@ -105,32 +105,25 @@ func gracefulExit() {
105105 os .Exit (0 )
106106}
107107
108- func handleSignals (exit chan bool ) {
109- signalCh := make (chan os.Signal , 1 )
110- signal .Notify (signalCh , os .Interrupt , syscall .SIGTERM )
111-
112- go func () {
113- // Set up the signals async so we can Start the agent
114- select {
115- case sig := <- signalCh :
116- log .Infof ("Received signal '%s', shutting down..." , sig )
117- signalCh <- nil
118- exit <- true
119- default :
120- // continue
108+ // HandleSignals tells us whether we should exit.
109+ func HandleSignals (exit chan bool ) {
110+ sigIn := make (chan os.Signal , 100 )
111+ signal .Notify (sigIn , syscall .SIGINT , syscall .SIGTERM , syscall .SIGPIPE )
112+ // unix only in all likelihood; but we don't care.
113+ for sig := range sigIn {
114+ switch sig {
115+ case syscall .SIGINT , syscall .SIGTERM :
116+ log .Infof ("Caught signal '%s'; terminating." , sig )
117+ close (exit )
118+ return
119+ case syscall .SIGPIPE :
120+ // By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
121+ // Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
122+ // We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
123+ // See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
124+ continue
121125 }
122- }()
123-
124- // By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
125- // Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
126- // We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
127- sigpipeCh := make (chan os.Signal , 1 )
128- signal .Notify (sigpipeCh , syscall .SIGPIPE )
129- go func () {
130- for range sigpipeCh {
131- // do nothing
132- }
133- }()
126+ }
134127}
135128
136129// versionString returns the version information filled in at build time
0 commit comments