From eeec603975eb436bfa48a059945d678bee02b009 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 12 Jun 2025 08:56:55 -0700 Subject: [PATCH] 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. --- .changelog/26023.txt | 3 +++ command/agent/command.go | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 .changelog/26023.txt diff --git a/.changelog/26023.txt b/.changelog/26023.txt new file mode 100644 index 000000000..4b5fa2594 --- /dev/null +++ b/.changelog/26023.txt @@ -0,0 +1,3 @@ +```release-note:bug +agent: Fixed bug where agent would exit early from graceful shutdown when managed by systemd +``` diff --git a/command/agent/command.go b/command/agent/command.go index ceb3115ca..0ffc96c7d 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -1007,13 +1007,25 @@ func (c *Command) terminateGracefully(signalCh chan os.Signal, sdSock io.Writer) delay := time.NewTimer(timeout) - // Wait for leave or another signal - select { - case <-signalCh: - return 1 - case <-delay.C: - return 1 - case <-gracefulCh: + // Wait for leave or another signal to be received + for { + select { + 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