Files
seaweedfs/weed/s3api/s3api_object_handlers_head_probe_test.go
T
Yip LeoandClaude Opus 5.5 5adfc23e85 s3api: serve a 0-byte file on HEAD when its child probe fails (#12824)
HEAD of a 0-byte file lists one child to tell an implicit directory from
an empty file, and hasChildren answers true when that listing fails. The
listing callers want that, since keeping an entry is their safe side, but
HEAD then answers 404 NoSuchKey for a file it has just found. A copy tool
that HEADs the key it just wrote reports the copy as failed; rclone logs
"Failed to copy: object not found" for every empty __init__.py or lock
file whose probe failed, although the object is in place.

The probe fails like this whenever the request context is already
canceled while the client still waits. net/http cancels a connection's
context when its background read fails, and the read deadline the
activity-timeout listener sets can expire during a long request that
neither reads nor writes, such as a large server-side copy. The
connection stays open and keeps serving keep-alive requests, each with a
context canceled from the start, so every 0-byte HEAD on it answered 404.

HEAD now asks zeroByteFileHasChildren, which answers false when the probe
fails, and the file is served. hasChildren keeps answering true for the
listings. A probe on an already-ended context returns at once instead of
failing every filer of the failover walk and counting each as a failure.

TestZeroByteFileHasChildrenServesTheFileWhenTheProbeFails covers a real
child, no child, a canceled context and a filer error.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 12:49:44 -08:00

96 lines
2.9 KiB
Go

package s3api
import (
"context"
"sync"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// childProbeFiler answers ListEntries from fixed directory contents, or with
// listErr, and counts the listings it served.
type childProbeFiler struct {
filer_pb.UnimplementedSeaweedFilerServer
mu sync.Mutex
children map[string][]string
listErr error
lists int
}
func (f *childProbeFiler) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) error {
f.mu.Lock()
f.lists++
listErr := f.listErr
names := append([]string(nil), f.children[req.Directory]...)
f.mu.Unlock()
if listErr != nil {
return listErr
}
for _, name := range names {
if err := stream.Send(&filer_pb.ListEntriesResponse{Entry: &filer_pb.Entry{Name: name}}); err != nil {
return err
}
}
return nil
}
func (f *childProbeFiler) setListErr(err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.listErr = err
}
func (f *childProbeFiler) listCount() int {
f.mu.Lock()
defer f.mu.Unlock()
return f.lists
}
// HEAD of a 0-byte file answers 404 only when the filer confirms children. A
// probe that cannot run or fails must not turn an existing file into a 404,
// while the listing keeps treating a failed probe as "has children".
func TestZeroByteFileHasChildrenServesTheFileWhenTheProbeFails(t *testing.T) {
filer := &childProbeFiler{children: map[string][]string{
"/buckets/b/dataset": {"part-0.parquet"},
}}
s3a := newPutTestServer(t, startFakeFiler(t, filer))
ctx := context.Background()
if !s3a.zeroByteFileHasChildren(ctx, "b", "dataset") {
t.Fatal("a 0-byte file with a child must answer as an implicit directory")
}
if s3a.zeroByteFileHasChildren(ctx, "b", "empty.lock") {
t.Fatal("a 0-byte file without children must be served")
}
if !s3a.hasChildren(ctx, "b", "dataset") || s3a.hasChildren(ctx, "b", "empty.lock") {
t.Fatal("hasChildren must report what the filer lists")
}
// The request context of a keep-alive connection can already be canceled
// while the client still waits for the answer.
canceled, cancel := context.WithCancel(ctx)
cancel()
before := filer.listCount()
if s3a.zeroByteFileHasChildren(canceled, "b", "empty.lock") {
t.Fatal("a probe on a canceled context must not answer an existing 0-byte file with 404")
}
if !s3a.hasChildren(canceled, "b", "empty.lock") {
t.Fatal("a listing must still keep an entry whose probe could not run")
}
if got := filer.listCount(); got != before {
t.Fatalf("a canceled context must not reach the filer, got %d listings", got-before)
}
filer.setListErr(status.Error(codes.Unavailable, "filer restarting"))
if s3a.zeroByteFileHasChildren(ctx, "b", "empty.lock") {
t.Fatal("a failed probe must not answer an existing 0-byte file with 404")
}
if !s3a.hasChildren(ctx, "b", "empty.lock") {
t.Fatal("a listing must still keep an entry whose probe failed")
}
}