From 9329b423dfc5f3537b90a0d03d034ff9b71df254 Mon Sep 17 00:00:00 2001 From: matthew-pilot Date: Sat, 30 May 2026 16:43:04 +0000 Subject: [PATCH] fix: handle conn.Write and fmt.Fprintf errors in echo and webserver examples (PILOT-299) Previously, conn.Write errors in go/echo/main.go and fmt.Fprintf errors in go/webserver/main.go were silently discarded. Since these are example programs that users copy, they should model correct error handling. Added log.Printf on write errors with a return (echo) or no-op continue (webserver HTTP handler where the framework handles teardown). --- go/echo/main.go | 10 ++++++++-- go/webserver/main.go | 10 +++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/go/echo/main.go b/go/echo/main.go index 531b930..72c5ccc 100644 --- a/go/echo/main.go +++ b/go/echo/main.go @@ -44,10 +44,16 @@ func main() { return } if *raw { - conn.Write(buf[:n]) + if _, err := conn.Write(buf[:n]); err != nil { + log.Printf("write: %v", err) + return + } } else { log.Printf("received from %s: %s", conn.RemoteAddr(), string(buf[:n])) - conn.Write(append([]byte("echo: "), buf[:n]...)) + if _, err := conn.Write(append([]byte("echo: "), buf[:n]...)); err != nil { + log.Printf("write: %v", err) + return + } } } }() diff --git a/go/webserver/main.go b/go/webserver/main.go index de67962..7400203 100644 --- a/go/webserver/main.go +++ b/go/webserver/main.go @@ -30,7 +30,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") - fmt.Fprintf(w, ` + if _, err := fmt.Fprintf(w, ` Pilot Protocol @@ -38,12 +38,16 @@ func main() {

This page is served over the Pilot Protocol overlay network.

You are connected to an agent at address: %s

-`, ln.Addr()) +`, ln.Addr()); err != nil { + log.Printf("write response: %v", err) + } }) mux.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok","protocol":"pilot","port":%d}`, *port) + if _, err := fmt.Fprintf(w, `{"status":"ok","protocol":"pilot","port":%d}`, *port); err != nil { + log.Printf("write response: %v", err) + } }) log.Printf("webserver listening on pilot port %d", *port)