carlos / platform Public

perf: a panic after the first byte is still recorded as a 500

The sample's status is 500 whenever the handler panicked; the headers
already sent and the first-byte time they were stamped with stay.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 1459c0877aefb592406c0cda99ffe9b7168eb08d parent d413a7f
2 files changed, +59 −22
  • internal/perf/middleware.go +9 −3
  • internal/perf/middleware_test.go +50 −19

CI failed — run details

diff --git a/internal/perf/middleware.go b/internal/perf/middleware.go
index a14ce2142..b284aae41 100644
--- a/internal/perf/middleware.go
+++ b/internal/perf/middleware.go
@@ -289,10 +289,16 @@ func (q *writer) finish(r *http.Request, screenIsDefault bool, lostRoute *sync.O
p := recover()
q.mu.Lock()
committed := q.committed
- if !committed && p != nil {
- q.committed = true
+ if p != nil {
+ // A 500 whenever the handler panicked, even after its headers
+ // went: a page cut off mid-body failed, whatever status it
+ // promised. Only the sample changes; the headers already sent,
+ // and the first-byte time they were stamped with, stay.
q.sample.Status = http.StatusInternalServerError
- q.sample.TTFB = ms(time.Since(q.start))
+ if !committed {
+ q.committed = true
+ q.sample.TTFB = ms(time.Since(q.start))
+ }
}
q.mu.Unlock()
if !committed && p == nil {
diff --git a/internal/perf/middleware_test.go b/internal/perf/middleware_test.go
index 1da404031..761784a7c 100644
--- a/internal/perf/middleware_test.go
+++ b/internal/perf/middleware_test.go
@@ -489,27 +489,58 @@ func (l lockedWriter) Write(b []byte) (int, error) {
return l.w.Write(b)
}
-// TestPanicIsRecordedAndReraised: not acceptance.
+// TestPanicIsRecordedAndReraised: not acceptance. A panic is recorded
+// as a 500 and re-raised with its own value, whether it came before the
+// first byte or after a write or a flush. Before, no header is written
+// (the recovery above owns the response); after, the headers already
+// sent and the first-byte time stay as they were.
func TestPanicIsRecordedAndReraised(t *testing.T) {
- mux := http.NewServeMux()
- mux.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) { panic("boom") })
- var c perf.Recorder
- h := perf.Middleware(&c, perf.Options{Logger: quiet})(mux)
- rec := httptest.NewRecorder()
- func() {
- defer func() {
- if p := recover(); p != "boom" {
- t.Fatalf("recovered %v, want boom", p)
+ for _, tc := range []struct {
+ name string
+ before func(w http.ResponseWriter)
+ sent bool
+ }{
+ {"before the first byte", func(http.ResponseWriter) {}, false},
+ {"after a write", func(w http.ResponseWriter) { io.WriteString(w, "partial") }, true},
+ {"after a flush", func(w http.ResponseWriter) { http.NewResponseController(w).Flush() }, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ mux := http.NewServeMux()
+ mux.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {
+ tc.before(w)
+ time.Sleep(20 * time.Millisecond)
+ panic("boom")
+ })
+ var c perf.Recorder
+ h := perf.Middleware(&c, perf.Options{Logger: quiet})(mux)
+ rec := httptest.NewRecorder()
+ func() {
+ defer func() {
+ if p := recover(); p != "boom" {
+ t.Fatalf("recovered %v, want boom", p)
+ }
+ }()
+ h.ServeHTTP(rec, httptest.NewRequest("GET", "/x", nil))
+ t.Fatal("ServeHTTP returned, want a panic")
+ }()
+ s := only(t, c.Snapshot())
+ if s.Status != http.StatusInternalServerError {
+ t.Fatalf("status = %d, want 500", s.Status)
}
- }()
- h.ServeHTTP(rec, httptest.NewRequest("GET", "/x", nil))
- t.Fatal("ServeHTTP returned, want a panic")
- }()
- if s := only(t, c.Snapshot()); s.Status != http.StatusInternalServerError {
- t.Fatalf("status = %d, want 500", s.Status)
- }
- if st := rec.Header().Values("Server-Timing"); len(st) != 0 {
- t.Fatalf("Server-Timing = %q, want none: the recovery above owns the response", st)
+ st := rec.Header().Values("Server-Timing")
+ if !tc.sent {
+ if len(st) != 0 {
+ t.Fatalf("Server-Timing = %q, want none: the recovery above owns the response", st)
+ }
+ return
+ }
+ if len(st) != 1 || rec.Code != http.StatusOK {
+ t.Fatalf("code %d, Server-Timing %q: want the 200 and the one header already sent, untouched", rec.Code, st)
+ }
+ if s.TTFB > s.Total-10 {
+ t.Fatalf("ttfb %v total %v: want the first-byte time kept from the write, not moved to the panic", s.TTFB, s.Total)
+ }
+ })
}
}