fix(14.2): WR-02 499 only for a 5xx caused by the client going away
Every 5xx became 499 once the request context was cancelled, so a panic or a real database error on a request the browser had abandoned passed the phase gate's 5xx scan. Now a 5xx is logged as 499 only when the client had already gone when the status was written and the handler did not panic; recoverJSON and recoverBare mark the access log entry. Every record of an abandoned request carries client_gone=true. The gate self-test plants a 5xx with client_gone=true and must refuse it.
This commit is contained in:
@@ -128,16 +128,63 @@ func TestAccessLogSmoke(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A 5xx written after the client cancelled the request is logged as 499;
|
||||
// a 5xx on a live request keeps its status.
|
||||
// TestAccessLogClientClosedRequest: a 5xx is logged as 499 only when the
|
||||
// client had already gone (the request context ended with context.Canceled)
|
||||
// when the status was written and the handler did not panic. A 5xx written
|
||||
// before the client went away keeps its status, and a recovered panic is
|
||||
// always 500, so a grep for 5xx still finds every real server error. Every
|
||||
// record of a request whose client went away carries client_gone=true.
|
||||
func TestAccessLogClientClosedRequest(t *testing.T) {
|
||||
type outcome struct {
|
||||
status float64
|
||||
clientGone bool
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
cancel bool
|
||||
want float64
|
||||
name string
|
||||
handler func(cancel context.CancelFunc) http.Handler
|
||||
want outcome
|
||||
}{
|
||||
{"cancelled", true, 499},
|
||||
{"live", false, 500},
|
||||
{"5xx after the client went away", func(cancel context.CancelFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
cancel()
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})
|
||||
}, outcome{499, true}},
|
||||
{"live 5xx", func(context.CancelFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})
|
||||
}, outcome{500, false}},
|
||||
{"5xx before the client went away", func(cancel context.CancelFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
cancel()
|
||||
})
|
||||
}, outcome{502, true}},
|
||||
{"200 then the client went away", func(cancel context.CancelFunc) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte("ok"))
|
||||
cancel()
|
||||
})
|
||||
}, outcome{200, true}},
|
||||
{"5xx through the JSON recovery after the client went away", func(cancel context.CancelFunc) http.Handler {
|
||||
return recordPattern("GET /blog/posts/{id}", recoverJSON(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
cancel()
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})))
|
||||
}, outcome{499, true}},
|
||||
{"panic after the client went away (JSON group)", func(cancel context.CancelFunc) http.Handler {
|
||||
return recordPattern("GET /blog/posts/{id}", recoverJSON(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||
cancel()
|
||||
panic("blog: broken")
|
||||
})))
|
||||
}, outcome{500, true}},
|
||||
{"panic after the client went away (raw group)", func(cancel context.CancelFunc) http.Handler {
|
||||
return recordPattern("GET /blog/posts/{id}", recoverBare(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||
cancel()
|
||||
panic("blog: broken")
|
||||
})))
|
||||
}, outcome{500, true}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
app := backpack.New(writeHTTPConfig(t, "access_log: true\n"))
|
||||
@@ -147,17 +194,16 @@ func TestAccessLogClientClosedRequest(t *testing.T) {
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
h := accessLog(app, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
if tc.cancel {
|
||||
cancel()
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
h := accessLog(app, tc.handler(cancel))
|
||||
req := httptest.NewRequest(http.MethodGet, "/blog/posts/7", nil).WithContext(ctx)
|
||||
h.ServeHTTP(httptest.NewRecorder(), req)
|
||||
recs := accessRecords(t, &buf)
|
||||
if len(recs) != 1 || recs[0]["status"] != tc.want {
|
||||
t.Fatalf("records=%v, want one with status %v", recs, tc.want)
|
||||
if len(recs) != 1 {
|
||||
t.Fatalf("records = %v, want one", recs)
|
||||
}
|
||||
gone, hasGone := recs[0]["client_gone"]
|
||||
if recs[0]["status"] != tc.want.status || hasGone != tc.want.clientGone || (hasGone && gone != true) {
|
||||
t.Fatalf("record = %v, want status %v client_gone %t", recs[0], tc.want.status, tc.want.clientGone)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user