Merge pull request #25832 from jakecorrenti/handle-sigint
Handle machine start state when sent a signal
This commit is contained in:
commit
79a820ac3b
|
@ -6,10 +6,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containers/podman/v5/pkg/machine"
|
"github.com/containers/podman/v5/pkg/machine"
|
||||||
|
@ -440,6 +442,7 @@ func stopLocked(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *mach
|
||||||
func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDefine.MachineDirs, opts machine.StartOptions) error {
|
func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDefine.MachineDirs, opts machine.StartOptions) error {
|
||||||
defaultBackoff := 500 * time.Millisecond
|
defaultBackoff := 500 * time.Millisecond
|
||||||
maxBackoffs := 6
|
maxBackoffs := 6
|
||||||
|
signalChanClosed := false
|
||||||
|
|
||||||
mc.Lock()
|
mc.Lock()
|
||||||
defer mc.Unlock()
|
defer mc.Unlock()
|
||||||
|
@ -471,17 +474,41 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if the machine cannot continue starting due to a signal, ensure the state
|
||||||
|
// reflects the machine is no longer starting
|
||||||
|
signalChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
sig, ok := <-signalChan
|
||||||
|
if ok {
|
||||||
|
mc.Starting = false
|
||||||
|
logrus.Error("signal received when starting the machine: ", sig)
|
||||||
|
|
||||||
|
if err := mc.Write(); err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Set starting to true
|
// Set starting to true
|
||||||
mc.Starting = true
|
mc.Starting = true
|
||||||
if err := mc.Write(); err != nil {
|
if err := mc.Write(); err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set starting to false on exit
|
// Set starting to false on exit
|
||||||
defer func() {
|
defer func() {
|
||||||
mc.Starting = false
|
mc.Starting = false
|
||||||
if err := mc.Write(); err != nil {
|
if err := mc.Write(); err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !signalChanClosed {
|
||||||
|
signal.Stop(signalChan)
|
||||||
|
close(signalChan)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
gvproxyPidFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil)
|
gvproxyPidFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil)
|
||||||
|
@ -557,6 +584,11 @@ func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDe
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// now that the machine has transitioned into the running state, we don't need a goroutine listening for SIGINT or SIGTERM to handle state
|
||||||
|
signal.Stop(signalChan)
|
||||||
|
close(signalChan)
|
||||||
|
signalChanClosed = true
|
||||||
|
|
||||||
if err := proxyenv.ApplyProxies(mc); err != nil {
|
if err := proxyenv.ApplyProxies(mc); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue