From dfae51a4b8d8613467f12f616ca24800040008c5 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Tue, 27 Feb 2024 12:59:37 -0600 Subject: [PATCH] Clean up gvproxy if machine start fails Matt found a bug where if a machine start did not run to completion, a gvproxy was left around running. This gvproxy then subsequently stopped the next attempt to start. Signed-off-by: Brent Baude [NO NEW TESTS NEEDED] --- pkg/machine/cleanup.go | 2 +- pkg/machine/hyperv/stubber.go | 6 +++--- pkg/machine/shim/host.go | 20 ++++++++++++++++++-- pkg/machine/wsl/stubber.go | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/machine/cleanup.go b/pkg/machine/cleanup.go index ff23846cf7..97e45b2c6a 100644 --- a/pkg/machine/cleanup.go +++ b/pkg/machine/cleanup.go @@ -55,7 +55,7 @@ func (c *CleanupCallback) clean() { } } -func InitCleanup() CleanupCallback { +func CleanUp() CleanupCallback { return CleanupCallback{ Funcs: []func() error{}, } diff --git a/pkg/machine/hyperv/stubber.go b/pkg/machine/hyperv/stubber.go index 03838a9aee..765049f361 100644 --- a/pkg/machine/hyperv/stubber.go +++ b/pkg/machine/hyperv/stubber.go @@ -45,7 +45,7 @@ func (h HyperVStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineC var ( err error ) - callbackFuncs := machine.InitCleanup() + callbackFuncs := machine.CleanUp() defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() @@ -182,7 +182,7 @@ func (h HyperVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func( return nil, nil, err } - callbackFuncs := machine.InitCleanup() + callbackFuncs := machine.CleanUp() defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() @@ -384,7 +384,7 @@ func (h HyperVStubber) PostStartNetworking(mc *vmconfigs.MachineConfig, noInfo b err error executable string ) - callbackFuncs := machine.InitCleanup() + callbackFuncs := machine.CleanUp() defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index 401215cde7..06ff9a207f 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -70,7 +70,7 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) (*vmconfigs.M imagePath *machineDefine.VMFile ) - callbackFuncs := machine.InitCleanup() + callbackFuncs := machine.CleanUp() defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() @@ -350,15 +350,31 @@ func Stop(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineDef return nil } -func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, _ *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 maxBackoffs := 6 + gvproxyPidFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil) + if err != nil { + return err + } + // start gvproxy and set up the API socket forwarding forwardSocketPath, forwardingState, err := startNetworking(mc, mp) if err != nil { return err } + + callBackFuncs := machine.CleanUp() + defer callBackFuncs.CleanIfErr(&err) + go callBackFuncs.CleanOnSignal() + + // Clean up gvproxy if start fails + cleanGV := func() error { + return machine.CleanupGVProxy(*gvproxyPidFile) + } + callBackFuncs.Add(cleanGV) + // if there are generic things that need to be done, a preStart function could be added here // should it be extensive diff --git a/pkg/machine/wsl/stubber.go b/pkg/machine/wsl/stubber.go index e1d6b483d3..0e994727a8 100644 --- a/pkg/machine/wsl/stubber.go +++ b/pkg/machine/wsl/stubber.go @@ -31,7 +31,7 @@ func (w WSLStubber) CreateVM(opts define.CreateVMOpts, mc *vmconfigs.MachineConf err error ) // cleanup half-baked files if init fails at any point - callbackFuncs := machine.InitCleanup() + callbackFuncs := machine.CleanUp() defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() mc.WSLHypervisor = new(vmconfigs.WSLConfig)