mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
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:
3
.changelog/26018.txt
Normal file
3
.changelog/26018.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
```release-note:bug
|
||||
agent: Fixed a bug to prevent a possible panic during graceful shutdown
|
||||
```
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user