diff --git a/.changelog/26018.txt b/.changelog/26018.txt new file mode 100644 index 000000000..c8f356843 --- /dev/null +++ b/.changelog/26018.txt @@ -0,0 +1,3 @@ +```release-note:bug +agent: Fixed a bug to prevent a possible panic during graceful shutdown +``` diff --git a/command/agent/command.go b/command/agent/command.go index 0ffc96c7d..b6b541f54 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -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)