Repository navigation
fix(vz): stop the VM even when CanRequestStop is false #5436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //go:build darwin && !no_vz | ||
|
|
||
| // SPDX-FileCopyrightText: Copyright The Lima Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package vz | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
|
|
||
| "github.com/lima-vm/lima/v2/pkg/limatype" | ||
| "github.com/lima-vm/lima/v2/pkg/limatype/filenames" | ||
| ) | ||
|
|
||
| func TestWaitForStopped(t *testing.T) { | ||
| t.Run("returns nil after the VM has stopped", func(t *testing.T) { | ||
| l := &LimaVzDriver{machine: &virtualMachineWrapper{}} | ||
| go func() { | ||
| l.machine.mu.Lock() | ||
| l.machine.stopped = true | ||
| l.machine.mu.Unlock() | ||
| }() | ||
| assert.NilError(t, l.waitForStopped(10*time.Second)) | ||
| }) | ||
|
|
||
| t.Run("returns an error when the VM is still running", func(t *testing.T) { | ||
| l := &LimaVzDriver{machine: &virtualMachineWrapper{}} | ||
| assert.ErrorContains(t, l.waitForStopped(600*time.Millisecond), "timeout") | ||
| }) | ||
| } | ||
|
|
||
| func TestRemovePIDFile(t *testing.T) { | ||
| dir := t.TempDir() | ||
| l := &LimaVzDriver{ | ||
| Instance: &limatype.Instance{ | ||
| Dir: dir, | ||
| Config: &limatype.LimaYAML{VMType: new(limatype.VZ)}, | ||
| }, | ||
| } | ||
| pidFile := filepath.Join(dir, filenames.PIDFile(limatype.VZ)) | ||
| assert.NilError(t, os.WriteFile(pidFile, []byte("42\n"), 0o644)) | ||
|
|
||
| l.removePIDFile() | ||
| _, err := os.Stat(pidFile) | ||
| assert.Assert(t, errors.Is(err, os.ErrNotExist)) | ||
|
|
||
| // Removing a PID file that does not exist is not an error. | ||
| l.removePIDFile() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this explicitly now ??
If requestStop is not supported anyway we have ssh shutdown this should cover almost all cases. Even that doesn't work stop will end in error (having pid in this case is better right ??).
Just my take please let me know what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With vz the PID file holds the PID of the process that runs the VM in-process:
vz.pidis written withos.Getpid()invm_darwin.go.Stop()is only called on the way outstartRoutinesAndWait()athostagent.go:530, and for an external driver the driver process is stopped on the next line byserver.Stop().So once
Stop()returns, with or without an error, thatPIDis dead and the file cannot mean "the VM is still running".The cleanup is also not only about the error path: nothing ever removes
vz.pid, not even after a successful stop. qemu removes it inkillQEMU()and krunkit in the goroutine that waits for the process; vz is the only driver that doesn't. It goes unnoticed today becauseReadPIDFile()drops the PID file of a dead process, so within one boot it self-heals. After a reboot the PID may have been reused, and that is the broken state reported in #5087.