From f7eeb1d65c80b2f634b3ce406fa71ba1f0e078a2 Mon Sep 17 00:00:00 2001 From: Saleh Date: Sun, 4 Oct 2026 15:48:06 +0300 Subject: [PATCH] reverseproxy: log HTTP/3 client cancellations at debug level (#8135) --- .../reverseproxy/client_disconnect_test.go | 96 +++++++++++++++++++ .../caddyhttp/reverseproxy/reverseproxy.go | 20 +++- modules/caddyhttp/reverseproxy/streaming.go | 6 +- 3 files changed, 120 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/reverseproxy/client_disconnect_test.go b/modules/caddyhttp/reverseproxy/client_disconnect_test.go index a56478e56..eefcfffe8 100644 --- a/modules/caddyhttp/reverseproxy/client_disconnect_test.go +++ b/modules/caddyhttp/reverseproxy/client_disconnect_test.go @@ -2,11 +2,19 @@ package reverseproxy import ( "context" + "errors" "net/http" "net/http/httptest" + "strings" "sync" "testing" + "github.com/quic-go/quic-go/http3" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" + + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/modules/caddyhttp" ) @@ -52,3 +60,91 @@ func TestClientDisconnectRecordsStatus(t *testing.T) { t.Errorf("expected status 499 after client disconnect, got %d", got) } } + +// failingWriter is a ResponseWriter whose body writes fail with err. +type failingWriter struct { + *httptest.ResponseRecorder + err error +} + +func (w failingWriter) Write([]byte) (int, error) { return 0, w.err } + +// failingReader is a response body from the backend whose reads fail with err. +type failingReader struct{ err error } + +func (r failingReader) Read([]byte) (int, error) { return 0, r.err } + +// TestClientCancelAbortLogLevel verifies that an HTTP/3 client canceling the +// request while the response is streamed is logged at debug level, while +// other errors that abort the response, including the same error coming from +// the backend, are still logged as warnings. +func TestClientCancelAbortLogLevel(t *testing.T) { + for _, tc := range []struct { + name string + err error + readErr bool // fail reading from the backend instead of writing to the client + want zapcore.Level + }{ + { + name: "http3 request canceled by client", + err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeRequestCanceled}, + want: zapcore.DebugLevel, + }, + { + name: "http3 request canceled locally", + err: &http3.Error{Remote: false, ErrorCode: http3.ErrCodeRequestCanceled}, + want: zapcore.WarnLevel, + }, + { + name: "other http3 error", + err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeInternalError}, + want: zapcore.WarnLevel, + }, + { + name: "other error", + err: errors.New("broken pipe"), + want: zapcore.WarnLevel, + }, + { + name: "http3 response canceled by backend", + err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeRequestCanceled}, + readErr: true, + want: zapcore.WarnLevel, + }, + } { + t.Run(tc.name, func(t *testing.T) { + core, logs := observer.New(zapcore.DebugLevel) + h := &Handler{logger: zap.New(core)} + + req := httptest.NewRequest(http.MethodGet, "/", nil) + req = req.WithContext(context.WithValue(req.Context(), caddyhttp.VarsCtxKey, map[string]any{})) + res := &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{}, + Body: fakeRWC{strings.NewReader("hello")}, + } + var rw http.ResponseWriter = failingWriter{ResponseRecorder: httptest.NewRecorder(), err: tc.err} + if tc.readErr { + res.Body = fakeRWC{failingReader{tc.err}} + rw = httptest.NewRecorder() + } + + func() { + defer func() { + if r := recover(); r != http.ErrAbortHandler { + t.Fatalf("expected panic with http.ErrAbortHandler, got %v", r) + } + }() + _ = h.finalizeResponse(rw, req, res, caddy.NewReplacer(), fakeStart, h.logger, "example.test:443") + }() + + entries := logs.FilterMessage("aborting with incomplete response").All() + if len(entries) != 1 { + t.Fatalf("expected 1 log entry, got %d", len(entries)) + } + if got := entries[0].Level; got != tc.want { + t.Errorf("log level = %s, want %s", got, tc.want) + } + }) + } +} diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index e1d56a6e0..7b09ab534 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -36,6 +36,7 @@ import ( "time" "github.com/dunglas/httpsfv" + "github.com/quic-go/quic-go/http3" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/net/http/httpguts" @@ -1452,7 +1453,13 @@ func (h *Handler) finalizeResponse( // we'll just log the error and abort the stream here and panic just as // the standard lib's proxy to propagate the stream error. // see issue https://github.com/caddyserver/caddy/issues/5951 - if c := logger.Check(zapcore.WarnLevel, "aborting with incomplete response"); c != nil { + lvl := zapcore.WarnLevel + if isClientCanceledH3(err) { + // the client canceled the request; expected and not actionable + // see issue https://github.com/caddyserver/caddy/issues/5766 + lvl = zapcore.DebugLevel + } + if c := logger.Check(lvl, "aborting with incomplete response"); c != nil { c.Write(zap.Error(err)) } // flush the buffer to ensure the client sees the partial response @@ -1494,6 +1501,17 @@ func (h *Handler) finalizeResponse( return nil } +// isClientCanceledH3 returns true if err is an HTTP/3 client canceling +// its request (H3_REQUEST_CANCELLED) while the response was being written. +// The same error read from an HTTP/3 backend is the backend's doing. +func isClientCanceledH3(err error) bool { + if !errors.Is(err, errWritingDownstream) { + return false + } + h3Err, ok := errors.AsType[*http3.Error](err) + return ok && h3Err.Remote && h3Err.ErrorCode == http3.ErrCodeRequestCanceled +} + // tryAgain takes the time that the handler was initially invoked, // the amount of retries already performed, as well as any error // currently obtained, and the request being tried, and returns diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go index d84262b5c..8f0569cb8 100644 --- a/modules/caddyhttp/reverseproxy/streaming.go +++ b/modules/caddyhttp/reverseproxy/streaming.go @@ -464,6 +464,10 @@ func (h Handler) copyResponse(dst http.ResponseWriter, src io.Reader, flushInter return err } +// errWritingDownstream wraps errors from writing the response to the client, +// to tell them apart from errors reading the response from the backend. +var errWritingDownstream = errors.New("writing") + // copyBuffer returns any write errors or non-EOF read errors, and the amount // of bytes written. func (h Handler) copyBuffer(dst io.Writer, src io.Reader, buf []byte, logger *zap.Logger) (int64, error) { @@ -504,7 +508,7 @@ func (h Handler) copyBuffer(dst io.Writer, src io.Reader, buf []byte, logger *za ) } if werr != nil { - return written, fmt.Errorf("writing: %w", werr) + return written, fmt.Errorf("%w: %w", errWritingDownstream, werr) } if nr != nw { return written, io.ErrShortWrite