From 7562f4ccdb62a27274914308847a8956f926f4ba Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 25 Jun 2024 15:44:48 +0200 Subject: [PATCH] pkg/machine/apple: machine stop timeout The current timeout was not long enough. Systemd default is 90s so we should wait for at least that long. Also it really doesn't make sense to throw an error we saying we failed waiting for stop. We should hard terminate the VM in case a graceful shutdown did not happen. Fixes #22515 Signed-off-by: Paul Holzinger --- pkg/machine/apple/vfkit/helper.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/pkg/machine/apple/vfkit/helper.go b/pkg/machine/apple/vfkit/helper.go index 07b1566f70..f971129b16 100644 --- a/pkg/machine/apple/vfkit/helper.go +++ b/pkg/machine/apple/vfkit/helper.go @@ -6,7 +6,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io" "net/http" "time" @@ -89,33 +88,28 @@ func (vf *Helper) stateChange(newState rest.StateChange) error { } func (vf *Helper) Stop(force, wait bool) error { - waitDuration := time.Millisecond * 10 - // TODO Add ability to wait until stopped + state := rest.Stop if force { - if err := vf.stateChange(rest.HardStop); err != nil { - return err - } - } else { - if err := vf.stateChange(rest.Stop); err != nil { - return err - } + state = rest.HardStop + } + if err := vf.stateChange(state); err != nil { + return err } if !wait { return nil } - waitErr := fmt.Errorf("failed waiting for vm to stop") - // Backoff to wait on the machine shutdown - for i := 0; i < 11; i++ { + waitDuration := time.Millisecond * 500 + // Wait up to 90s then hard force off + for i := 0; i < 180; i++ { _, err := vf.getRawState() if err != nil || errors.Is(err, unix.ECONNREFUSED) { - waitErr = nil - break + return nil } - waitDuration *= 2 - logrus.Debugf("backoff wait time: %s", waitDuration.String()) time.Sleep(waitDuration) } - return waitErr + logrus.Warn("Failed to gracefully stop machine, performing hard stop") + // we waited long enough do a hard stop + return vf.stateChange(rest.HardStop) } // Helper describes the use of vfkit: cmdline and endpoint