command: prevent panic on graceful shutdown (#26018)

When performing a graceful shutdown a channel is used to wait for
the agent to leave. The channel is closed when the agent leaves
successfully, but it also is closed within a deferral. If the
agent successfully leaves and closes the channel, a panic will
occur when the channel is closed the second time within the
deferral. To prevent this from occurring, the channel closing
is wrapped within a `OnceFunc` so the channel is only closed
once.
This commit is contained in:
Chris Roberts
2025-06-12 09:35:57 -07:00
committed by GitHub
parent eeec603975
commit 4dbf645bf7
2 changed files with 7 additions and 2 deletions

3
.changelog/26018.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
agent: Fixed a bug to prevent a possible panic during graceful shutdown
```

View File

@@ -16,6 +16,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
@@ -983,7 +984,8 @@ func (c *Command) terminateGracefully(signalCh chan os.Signal, sdSock io.Writer)
sdNotify(sdSock, sdStopping)
gracefulCh := make(chan struct{})
defer close(gracefulCh)
gracefulClose := sync.OnceFunc(func() { close(gracefulCh) })
defer gracefulClose()
timeout := gracefulTimeout
@@ -1002,7 +1004,7 @@ func (c *Command) terminateGracefully(signalCh chan os.Signal, sdSock io.Writer)
c.Ui.Error(fmt.Sprintf("Error: %s", err))
return
}
close(gracefulCh)
gracefulClose()
}()
delay := time.NewTimer(timeout)