s3: answer ClientDisconnected for a GET the client aborted before the first byte (#12707)

* s3: answer ClientDisconnected for a GET the client aborted before the first byte

Fixes #12706

* s3: tell a gone client on TLS and HTTP/2 too, and not on a connection the server closed
This commit is contained in:
Konstantin Adzer authored and GitHub committed 2026-11-17 17:15:00 -08:00
1 parent c91da3d1f8
commit 91eaabf371
4 files changed
+117 -9

No files matched your search

+75 -1
View File
@@ -14,6 +14,7 @@ import (
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
@@ -54,7 +55,7 @@ func TestIdleTimeoutCancelStillAnswersConnectedClient(t *testing.T) {
if err != nil {
t.Fatal(err)
}
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server := &http.Server{ConnContext: util.ConnContext, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// a first read stalled past the idle timeout
<-r.Context().Done()
// only the read side should be short; a write timing out would fail the
@@ -84,6 +85,79 @@ func TestIdleTimeoutCancelStillAnswersConnectedClient(t *testing.T) {
}
}
// TestClientGoneBeforeFirstByteIsNotAnInternalError checks that a canceled
// stream whose client went away is answered ClientDisconnected, so an aborted
// download does not count as a server error.
func TestClientGoneBeforeFirstByteIsNotAnInternalError(t *testing.T) {
clientCancels := func(cancel context.CancelFunc, _ *httptest.Server) { cancel() }
serverCloses := func(_ context.CancelFunc, server *httptest.Server) { server.CloseClientConnections() }
clientDisconnected := s3err.GetAPIError(s3err.ErrClientDisconnected).HTTPStatusCode
tests := []struct {
name string
start func(*httptest.Server)
abort func(context.CancelFunc, *httptest.Server)
want int
}{
{"client closes the connection", (*httptest.Server).Start, clientCancels, clientDisconnected},
{"client closes the TLS connection", (*httptest.Server).StartTLS, clientCancels, clientDisconnected},
{"client resets the HTTP/2 stream", func(server *httptest.Server) {
server.EnableHTTP2 = true
server.StartTLS()
}, clientCancels, clientDisconnected},
{"server closes the connection", (*httptest.Server).Start, serverCloses, http.StatusInternalServerError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
listener, err := util.NewListener("127.0.0.1:0", time.Minute)
if err != nil {
t.Fatal(err)
}
entered := make(chan struct{})
status := make(chan int, 1)
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(entered)
<-r.Context().Done()
rec := httptest.NewRecorder()
writeStreamErrorResponse(rec, r, r.Context().Err())
status <- rec.Code
}))
server.Listener.Close()
server.Listener = listener
server.Config.ConnContext = util.ConnContext
tt.start(server)
defer server.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/bucket/object", nil)
if err != nil {
t.Fatal(err)
}
go func() {
if resp, err := server.Client().Do(req); err == nil {
resp.Body.Close()
}
}()
select {
case <-entered:
case <-time.After(5 * time.Second):
t.Fatal("the request never reached the handler")
}
tt.abort(cancel, server)
select {
case got := <-status:
if got != tt.want {
t.Fatalf("status = %d, want %d", got, tt.want)
}
case <-time.After(5 * time.Second):
t.Fatal("the request context was never canceled")
}
})
}
}
// cancelingLookupFiler cancels the request context while the first chunk
// lookup is in flight, as the idle timeout does to a client still connected.
type cancelingLookupFiler struct {