From 0f5be4baa56283baa3e71ea7074e7ad2b817d6d1 Mon Sep 17 00:00:00 2001 From: hamistao Date: Tue, 3 Sep 2024 11:19:20 -0300 Subject: [PATCH] lxd/instance/drivers: Handle websocket closing When the VM disconnects due to a stop/reboot, the socket is closed and the error we get is a `connection reset by peer` or a `Unexpected EOF` instead of a EOF. So this helps handle these errors similarly to make the command exit cleanly in these scenarios. Since both errors are of type errorString, the only way to check it is to see if they contains the expected substring. Signed-off-by: hamistao --- lxd/instance/drivers/driver_qemu_cmd.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lxd/instance/drivers/driver_qemu_cmd.go b/lxd/instance/drivers/driver_qemu_cmd.go index 8e42e6710844..845486de56d6 100644 --- a/lxd/instance/drivers/driver_qemu_cmd.go +++ b/lxd/instance/drivers/driver_qemu_cmd.go @@ -5,6 +5,8 @@ import ( "fmt" "io" "strconv" + "strings" + "syscall" "golang.org/x/sys/unix" @@ -80,7 +82,9 @@ 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) { + // 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 }