Fix shutdown UX: nohup wrapper prevents SSH session drop during host shutdown
When the host shuts down, sshd is killed before sending exit-status, causing Go ssh library to return "wait: remote command exited without exit status" — treated as failure even though the shutdown worked. Changes: - shutdown.go: wrap shutdown-like commands with nohup so the SSH session exits cleanly before sshd is killed by shutdown - IsShutdownCommand() helper detects shutdown/poweroff/halt/reboot commands - handlers_machines.go: classify expected shutdown-side-effect errors (no exit status, connection refused/reset) as success so the UI shows a green toast instead of a false error
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -282,7 +283,36 @@ func (h *MachineHandler) Shutdown(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeJSON(w, ShutdownResponse{Success: false, Error: err.Error()})
|
writeJSON(w, ShutdownResponse{Success: false, Error: err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSON(w, ShutdownResponse{Success: result.Success, Output: result.Output, Error: result.Error})
|
|
||||||
|
responseSuccess := result.Success
|
||||||
|
responseOutput := result.Output
|
||||||
|
responseError := result.Error
|
||||||
|
|
||||||
|
if !responseSuccess && m.ShutdownCommand != "" && sshmanager.IsShutdownCommand(m.ShutdownCommand) {
|
||||||
|
if isExpectedShutdownError(result.Error) {
|
||||||
|
responseSuccess = true
|
||||||
|
responseError = ""
|
||||||
|
if responseOutput == "" {
|
||||||
|
responseOutput = "shutdown command sent (host session terminated as expected)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, ShutdownResponse{
|
||||||
|
Success: responseSuccess,
|
||||||
|
Output: responseOutput,
|
||||||
|
Error: responseError,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isExpectedShutdownError(errMsg string) bool {
|
||||||
|
if errMsg == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return strings.Contains(errMsg, "remote command exited without exit status") ||
|
||||||
|
strings.Contains(errMsg, "connection refused") ||
|
||||||
|
strings.Contains(errMsg, "connection reset by peer") ||
|
||||||
|
strings.Contains(errMsg, "use of closed network connection")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MachineHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
|
func (h *MachineHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +14,20 @@ type ShutdownResult struct {
|
|||||||
Error string
|
Error string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsShutdownCommand(cmd string) bool {
|
||||||
|
c := strings.TrimSpace(strings.ToLower(cmd))
|
||||||
|
for _, p := range []string{
|
||||||
|
"shutdown", "poweroff", "halt", "reboot",
|
||||||
|
"sudo shutdown", "sudo poweroff", "sudo halt", "sudo reboot",
|
||||||
|
"systemctl poweroff", "systemctl halt", "systemctl reboot",
|
||||||
|
} {
|
||||||
|
if strings.HasPrefix(c, p) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool, command string) (*ShutdownResult, error) {
|
func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyPath, knownHostsPath string, strictHostKeyChecking bool, command string) (*ShutdownResult, error) {
|
||||||
conn, _, _, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
|
conn, _, _, err := dialSSH(ctx, host, port, user, privKeyPath, knownHostsPath, strictHostKeyChecking)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,12 +45,20 @@ func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyP
|
|||||||
session.Stdout = &stdout
|
session.Stdout = &stdout
|
||||||
session.Stderr = &stderr
|
session.Stderr = &stderr
|
||||||
|
|
||||||
|
effectiveCmd := command
|
||||||
|
if IsShutdownCommand(command) {
|
||||||
|
effectiveCmd = fmt.Sprintf(
|
||||||
|
"nohup %s >/dev/null 2>&1 </dev/null & sleep 1; echo shutdown_initiated",
|
||||||
|
command,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
done := make(chan error, 1)
|
done := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
done <- session.Run(command)
|
done <- session.Run(effectiveCmd)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -53,7 +76,7 @@ func RunRemoteCommand(ctx context.Context, host string, port int, user, privKeyP
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return &ShutdownResult{
|
return &ShutdownResult{
|
||||||
Success: false,
|
Success: false,
|
||||||
Error: "command timed out after 15 seconds (machine may be shutting down)",
|
Error: "command timed out after 15 seconds",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user