Skip to content

Commit 2426745

Browse files
committed
Handle signal preventing Start from completing
In the instance where the user sends a signal, such as SIGINT (Ctl-c) when a Podman Machine is in the middle of starting, make sure the state doesn't get stuck in the "Currently Starting" status. Resolves: #24416 Signed-off-by: Jake Correnti <[email protected]>
1 parent 20e1b9d commit 2426745

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pkg/machine/shim/host.go

+32
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"os/signal"
910
"path"
1011
"path/filepath"
1112
"runtime"
1213
"strings"
14+
"syscall"
1315
"time"
1416

1517
"github.com/containers/podman/v5/pkg/machine"
@@ -440,6 +442,7 @@ func stopLocked(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *mach
440442
func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDefine.MachineDirs, opts machine.StartOptions) error {
441443
defaultBackoff := 500 * time.Millisecond
442444
maxBackoffs := 6
445+
signalChanClosed := false
443446

444447
mc.Lock()
445448
defer mc.Unlock()
@@ -471,17 +474,41 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDe
471474
}
472475
}
473476

477+
// if the machine cannot continue starting due to a signal, ensure the state
478+
// reflects the machine is no longer starting
479+
signalChan := make(chan os.Signal, 1)
480+
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
481+
go func() {
482+
sig, ok := <-signalChan
483+
if ok {
484+
mc.Starting = false
485+
logrus.Error("signal received when starting the machine: ", sig)
486+
487+
if err := mc.Write(); err != nil {
488+
logrus.Error(err)
489+
}
490+
491+
os.Exit(1)
492+
}
493+
}()
494+
474495
// Set starting to true
475496
mc.Starting = true
476497
if err := mc.Write(); err != nil {
477498
logrus.Error(err)
478499
}
500+
479501
// Set starting to false on exit
480502
defer func() {
481503
mc.Starting = false
482504
if err := mc.Write(); err != nil {
483505
logrus.Error(err)
484506
}
507+
508+
if !signalChanClosed {
509+
signal.Stop(signalChan)
510+
close(signalChan)
511+
}
485512
}()
486513

487514
gvproxyPidFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil)
@@ -557,6 +584,11 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDe
557584
return errors.New(msg)
558585
}
559586

587+
// now that the machine has transitioned into the running state, we don't need a goroutine listening for SIGINT or SIGTERM to handle state
588+
signal.Stop(signalChan)
589+
close(signalChan)
590+
signalChanClosed = true
591+
560592
if err := proxyenv.ApplyProxies(mc); err != nil {
561593
return err
562594
}

0 commit comments

Comments
 (0)