Skip to content

Commit

Permalink
docd: don't panic on broken pipe errors (#155)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jonathaningram authored Nov 20, 2023
1 parent 9cc631a commit 0865213
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"syscall"

"cloud.google.com/go/errorreporting"

Expand Down Expand Up @@ -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))
}
}

0 comments on commit 0865213

Please sign in to comment.