Skip to content

Commit

Permalink
Improve VM shutdown (#13875)
Browse files Browse the repository at this point in the history
Includes cherry picks from lxc/incus#362
  • Loading branch information
tomponline authored Sep 7, 2024
2 parents d561a6a + 0f5be4b commit dbefa5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lxd/instance/drivers/driver_qemu_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"strconv"
"strings"
"syscall"

"golang.org/x/sys/unix"

Expand All @@ -13,6 +15,9 @@ import (
"github.com/canonical/lxd/shared/logger"
)

// ErrExecDisconnected is returned when the guest disconnects the exec session.
var ErrExecDisconnected = fmt.Errorf("Disconnected")

// Cmd represents a running command for an Qemu VM.
type qemuCmd struct {
attachedChildPid int
Expand Down Expand Up @@ -77,8 +82,10 @@ func (c *qemuCmd) Wait() (int, error) {
// Error of type EOF indicates the session ended unexpectedly,
// so we inform the client of the disconnection with a more
// descriptive message.
if errors.Is(err, io.EOF) {
return exitStatus, fmt.Errorf("Disconnected")
// The error can be different depending on why the VM disconnected
// so we handle these cases similarly.
if errors.Is(err, io.EOF) || strings.Contains(err.Error(), io.ErrUnexpectedEOF.Error()) || strings.Contains(err.Error(), syscall.ECONNRESET.Error()) {
return exitStatus, ErrExecDisconnected
}

return exitStatus, err
Expand Down
8 changes: 8 additions & 0 deletions lxd/instance_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/canonical/lxd/lxd/cluster"
"github.com/canonical/lxd/lxd/db/operationtype"
"github.com/canonical/lxd/lxd/instance"
"github.com/canonical/lxd/lxd/instance/drivers"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/lxd/operations"
"github.com/canonical/lxd/lxd/request"
Expand Down Expand Up @@ -289,7 +290,14 @@ func (s *execWs) Do(op *operations.Operation) error {
_ = pty.Close()
}

// Make VM disconnections (shutdown/reboot) match containers.
if cmdErr == drivers.ErrExecDisconnected {
cmdResult = 129
cmdErr = nil
}

metadata := shared.Jmap{"return": cmdResult}

err = op.ExtendMetadata(metadata)
if err != nil {
return err
Expand Down

0 comments on commit dbefa5e

Please sign in to comment.