Skip to content

Commit 908e2a1

Browse files
committed
fix: enhance terminal disconnection handling and optimize server build
- Improved terminal disconnection messages to include status codes and reasons. - Updated Dockerfile to optimize the Go server build with reduced binary size. - Refactored main function in main.go to simplify server startup and remove unnecessary signal handling.
1 parent 3d9ab44 commit 908e2a1

3 files changed

Lines changed: 13 additions & 38 deletions

File tree

src/lib/components/cloudshell/terminal-pane.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ import LoadingPane from './loading-pane.svelte';
177177
controller.setTerminalStatus('disconnected', 'Unable to reach the terminal runtime.');
178178
}
179179
};
180-
nextSocket.onclose = () => {
180+
nextSocket.onclose = (ev) => {
181181
if (sequence === reconnectSequence) {
182-
controller.setTerminalStatus('disconnected', 'Terminal connection closed.');
182+
const detail =
183+
ev.code === 1000
184+
? 'Terminal connection closed.'
185+
: `Terminal connection closed (code ${ev.code}${ev.reason ? `: ${ev.reason}` : ''}).`;
186+
controller.setTerminalStatus('disconnected', detail);
183187
}
184188
};
185189
} catch (error) {

worker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ COPY worker/container/go.mod ./
88
RUN go mod download github.com/creack/pty github.com/gorilla/websocket
99

1010
COPY worker/container/main.go ./
11-
RUN go build -o /server
11+
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /server
1212

1313
FROM alpine:3.20
1414

worker/container/main.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import (
99
"net/http"
1010
"os"
1111
"os/exec"
12-
"os/signal"
1312
"path/filepath"
1413
"strings"
1514
"sync"
16-
"syscall"
1715
"time"
1816

1917
"github.com/creack/pty"
@@ -848,9 +846,6 @@ func getRecordingHandler(w http.ResponseWriter, r *http.Request) {
848846
}
849847

850848
func main() {
851-
stop := make(chan os.Signal, 1)
852-
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
853-
854849
mux := http.NewServeMux()
855850
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
856851
if r.Header.Get("Upgrade") == "websocket" {
@@ -871,35 +866,11 @@ func main() {
871866
mux.HandleFunc("/api/recording/stop", stopRecordingHandler)
872867
mux.HandleFunc("/api/recording/get", getRecordingHandler)
873868

874-
server := &http.Server{
875-
Addr: ":8080",
876-
Handler: mux,
869+
// Block on main goroutine so PID 1 is the HTTP server (reliable for CF Containers waitForPort).
870+
// Bind all interfaces explicitly (some runtimes are picky vs implicit ":8080").
871+
const addr = "0.0.0.0:8080"
872+
log.Printf("CloudShell terminal server listening on %s", addr)
873+
if err := http.ListenAndServe(addr, mux); err != nil {
874+
log.Fatal(err)
877875
}
878-
879-
go func() {
880-
log.Printf("CloudShell terminal server listening on %s", server.Addr)
881-
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
882-
log.Fatalf("Server error: %v", err)
883-
}
884-
}()
885-
886-
<-stop
887-
log.Println("Shutting down server...")
888-
889-
if username, sessionID := currentRuntimeContext(); username != "" && sessionID != "" {
890-
if count, err := checkpointSession(username, sessionID); err != nil {
891-
log.Printf("Checkpoint on shutdown failed for %s/%s: %v", username, sessionID, err)
892-
} else {
893-
log.Printf("Checkpointed %d tabs on shutdown for %s/%s", count, username, sessionID)
894-
}
895-
}
896-
897-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
898-
defer cancel()
899-
900-
if err := server.Shutdown(ctx); err != nil {
901-
log.Printf("Shutdown error: %v", err)
902-
}
903-
904-
log.Println("Server stopped")
905876
}

0 commit comments

Comments
 (0)