From 08652136d5dca999c0bade466c906740f845bd6e Mon Sep 17 00:00:00 2001 From: Jonathan Ingram Date: Mon, 20 Nov 2023 13:42:55 +1100 Subject: [PATCH] docd: don't panic on broken pipe errors (#155) Example log entry that is panicking: > could not write to response (failed after 3965 bytes): write tcp 172.17.0.5:8080->172.17.0.4:61528: write: broken pipe --- docd/convert.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docd/convert.go b/docd/convert.go index 25b4825..cb5b1e4 100644 --- a/docd/convert.go +++ b/docd/convert.go @@ -4,11 +4,13 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "log/slog" "net/http" "os" + "syscall" "cloud.google.com/go/errorreporting" @@ -114,6 +116,12 @@ func (s *convertServer) respond(ctx context.Context, w http.ResponseWriter, r *h w.WriteHeader(code) n, err := io.Copy(w, buf) if err != nil { + // Avoid panicking on broken pipe errors. + // See https://gosamples.dev/broken-pipe/ + if errors.Is(err, syscall.EPIPE) { + s.l.DebugContext(ctx, err.Error(), "error", err) + return + } panic(fmt.Errorf("could not write to response (failed after %d bytes): %w", n, err)) } }