From 2a58e253129851ad1ee8c2ab2822ea7e6714f54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Thu, 12 Dec 2024 15:05:16 -0500 Subject: [PATCH 1/3] incusd/instance_console: Remove redundant (and unsafe) write MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The last write sequence is already handled by the ws package, this is just duplicated logic which may cause us to race with the ws package, crashing the daemon. Closes #1449 Signed-off-by: Stéphane Graber --- cmd/incusd/instance_console.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/incusd/instance_console.go b/cmd/incusd/instance_console.go index 9020e1c6d46..f1576cc1414 100644 --- a/cmd/incusd/instance_console.go +++ b/cmd/incusd/instance_console.go @@ -322,7 +322,6 @@ func (s *consoleWs) doConsole(op *operations.Operation) error { s.connsLock.Unlock() defer func() { - _ = consoleConn.WriteMessage(websocket.BinaryMessage, []byte("\n\r")) _ = consoleConn.Close() _ = ctrlConn.Close() }() From c1458ca06b2af583604b3a43f8cc8f2cb5715f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Thu, 12 Dec 2024 15:15:07 -0500 Subject: [PATCH 2/3] incus/console: Make sure we leave the console in a clean state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/incus/console.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/incus/console.go b/cmd/incus/console.go index 26e386b2368..abaf36a0fe4 100644 --- a/cmd/incus/console.go +++ b/cmd/incus/console.go @@ -213,6 +213,9 @@ func (c *cmdConsole) text(d incus.InstanceServer, name string) error { } close(consoleDisconnect) + + // Make sure we leave the user back to a clean prompt. + fmt.Printf("\r\n") }() // Attach to the instance console From 9485a99f54e876d960c964956943baae036484fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Thu, 12 Dec 2024 15:17:18 -0500 Subject: [PATCH 3/3] incusd/instance_console: Don't fail on failure to write reset sequence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This avoids tripping on a write error every time an instance is restarted. Signed-off-by: Stéphane Graber --- cmd/incusd/instance_console.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cmd/incusd/instance_console.go b/cmd/incusd/instance_console.go index f1576cc1414..550b61b3e79 100644 --- a/cmd/incusd/instance_console.go +++ b/cmd/incusd/instance_console.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -329,14 +330,10 @@ func (s *consoleWs) doConsole(op *operations.Operation) error { // Write a reset escape sequence to the console to cancel any ongoing reads to the handle // and then close it. This ordering is important, close the console before closing the // websocket to ensure console doesn't get stuck reading. - _, err = console.Write([]byte("\x1bc")) - if err != nil { - _ = console.Close() - return err - } + _, _ = console.Write([]byte("\x1bc")) err = console.Close() - if err != nil { + if err != nil && !errors.Is(err, os.ErrClosed) { return err }