command: prevent early exit from graceful shutdown (#26023)

While waiting for the agent to leave during a graceful shutdown
the wait can be interrupted immediately if another signal is
received. It is common that while waiting a `SIGPIPE` is received
from journald causing the wait to end early. This results in the
agent not finishing the leave process and reporting an error when
the process has stopped. Instead of allowing any signal to interrupt
the wait, the signal is checked for a `SIGPIPE` and if matched will
continue waiting.
This commit is contained in:
Chris Roberts
2025-06-12 08:56:55 -07:00
committed by GitHub
parent 0ddbc548a3
commit eeec603975
2 changed files with 22 additions and 7 deletions

3
.changelog/26023.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
agent: Fixed bug where agent would exit early from graceful shutdown when managed by systemd
```

View File

@@ -1007,15 +1007,27 @@ func (c *Command) terminateGracefully(signalCh chan os.Signal, sdSock io.Writer)
delay := time.NewTimer(timeout)
// Wait for leave or another signal
// Wait for leave or another signal to be received
for {
select {
case <-signalCh:
case sig := <-signalCh:
// If a SIGPIPE is received, ignore it and
// continue waiting
if sig == syscall.SIGPIPE {
c.agent.logger.Trace("caught SIGPIPE during graceful shutdown, ignoring")
continue
}
c.agent.logger.Trace("caught signal during graceful shutdown", "signal", sig)
return 1
case <-delay.C:
return 1
case <-gracefulCh:
}
break
}
return 0
}