backport of commit bdac8d9583b5b65267bb3f9d00d00be52024ce95 (#19163)

Co-authored-by: Luiz Aoqui <luiz@hashicorp.com>
This commit is contained in:
hc-github-team-nomad-core 2023-11-23 15:14:52 -06:00 committed by GitHub
parent 9f804b0b4c
commit 17e7e0d330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 11 deletions

3
.changelog/19154.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed a panic when the `nomad job restart` command received an interrupt signal while waiting for an answer
```

View File

@ -275,6 +275,17 @@ func (c *JobRestartCommand) Run(args []string) int {
c.client.SetNamespace(*job.Namespace)
}
// Handle SIGINT to prevent accidental cancellations of the long-lived
// restart loop. activeCh is blocked while a signal is being handled to
// prevent new work from starting while the user is deciding if they want
// to cancel the command or not.
activeCh := make(chan any)
c.sigsCh = make(chan os.Signal, 1)
signal.Notify(c.sigsCh, os.Interrupt)
defer signal.Stop(c.sigsCh)
go c.handleSignal(c.sigsCh, activeCh)
// Confirm that we should restart a multi-region job in a single region.
if job.IsMultiregion() && !c.autoYes && !c.shouldRestartMultiregion() {
c.Ui.Output("\nJob restart canceled.")
@ -329,17 +340,6 @@ func (c *JobRestartCommand) Run(args []string) int {
english.Plural(len(restartAllocs), "allocation", "allocations"),
)))
// Handle SIGINT to prevent accidental cancellations of the long-lived
// restart loop. activeCh is blocked while a signal is being handled to
// prevent new work from starting while the user is deciding if they want
// to cancel the command or not.
activeCh := make(chan any)
c.sigsCh = make(chan os.Signal, 1)
signal.Notify(c.sigsCh, os.Interrupt)
defer signal.Stop(c.sigsCh)
go c.handleSignal(c.sigsCh, activeCh)
// restartErr accumulates the errors that happen in each batch.
var restartErr *multierror.Error