From 50bc8fe6ba145936141adb2ad974dd5ff7d88f96 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 12:31:58 -0800 Subject: [PATCH 1/3] test(blockvolume): process-level ports are claimed host-wide; a child's failure shows its log Two CI jobs on one host (tp04 runs two runners) each run shards 1 to 4, so their shard slices of the test port ranges are the same: a port one job probed free could be bound by the other before its blockvolume did, and the child exited with status 1 before it was adopted. Every port picker now also takes a host-wide flock on /tmp/swblock-test-port-claims/.lock, held for the test process's life (the kernel drops it at exit). A child that exits, or does not log in time, now fails the test with the tail of its own log. Co-Authored-By: Claude Opus 5.5 --- .../blockvolume/head_size_integration_test.go | 2 +- .../planned_handoff_integration_test.go | 19 +++++- .../blockvolume/test_port_claim_other_test.go | 7 ++ .../cmd/blockvolume/test_port_claim_test.go | 50 ++++++++++++++ .../blockvolume/test_port_claim_unix_test.go | 68 +++++++++++++++++++ .../workspace_weed_integration_test.go | 3 + 6 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/test_port_claim_other_test.go create mode 100644 seaweed-block/cmd/blockvolume/test_port_claim_test.go create mode 100644 seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go diff --git a/seaweed-block/cmd/blockvolume/head_size_integration_test.go b/seaweed-block/cmd/blockvolume/head_size_integration_test.go index 0fb5d2fa14..8ebd6ecd7a 100644 --- a/seaweed-block/cmd/blockvolume/head_size_integration_test.go +++ b/seaweed-block/cmd/blockvolume/head_size_integration_test.go @@ -270,7 +270,7 @@ func headSzLoopbackAddr(t *testing.T) string { for i := 0; i < 500; i++ { lo, hi := testPortSpan(10000, 20000) p := lo + rand.Intn(hi-lo) - if handoffPorts.used[p] { + if handoffPorts.used[p] || !claimHostPort(p) { continue } addr := fmt.Sprintf("127.0.0.1:%d", p) diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go index ecb21f52c9..e1c6614ed2 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go @@ -520,7 +520,7 @@ func handoffAddress(t *testing.T) string { for i := 0; i < 500; i++ { lo, hi := testPortSpan(10000, 20000) p := lo + rand.Intn(hi-lo) - if handoffPorts.used[p] { + if handoffPorts.used[p] || !claimHostPort(p) { continue } addr := fmt.Sprintf("%s:%d", testListenHost(), p) @@ -646,6 +646,19 @@ func (d *daemon) line(s string) string { func (d *daemon) has(s string) bool { return d.line(s) != "" } +// tail is the last n lines of the child's log, for a failure report. +func (d *daemon) tail(n int) string { + b, err := os.ReadFile(d.log) + if err != nil { + return "(log unreadable: " + err.Error() + ")" + } + lines := strings.Split(strings.TrimRight(string(b), "\n"), "\n") + if len(lines) > n { + lines = lines[len(lines)-n:] + } + return strings.Join(lines, "\n") +} + // waitLog waits for a log line containing s and returns it. A child that // exits first fails the test at once. func (d *daemon) waitLog(t *testing.T, timeout time.Duration, s string) string { @@ -660,11 +673,11 @@ func (d *daemon) waitLog(t *testing.T, timeout time.Duration, s string) string { if l := d.line(s); l != "" { return l } - t.Fatalf("%s exited (%v) before logging %q", d.name, d.err, s) + t.Fatalf("%s exited (%v) before logging %q; its log ends:\n%s", d.name, d.err, s, d.tail(40)) default: } if time.Now().After(end) { - t.Fatalf("%s did not log %q within %s", d.name, s, timeout) + t.Fatalf("%s did not log %q within %s; its log ends:\n%s", d.name, s, timeout, d.tail(40)) } time.Sleep(100 * time.Millisecond) } diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_other_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_other_test.go new file mode 100644 index 0000000000..dbc6922137 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/test_port_claim_other_test.go @@ -0,0 +1,7 @@ +//go:build !unix + +package main_test + +// claimHostPort: the process-level tests run on Linux only; elsewhere every +// port is this process's to probe. +func claimHostPort(p int) bool { return true } diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_test.go new file mode 100644 index 0000000000..14ba97e341 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/test_port_claim_test.go @@ -0,0 +1,50 @@ +//go:build unix + +package main_test + +import ( + "os" + "os/exec" + "strconv" + "testing" +) + +// A port claimed by one process is refused to another on the same host, and +// free again once the first exits. The other process is this test binary +// started again with SWBLOCK_TEST_PORT_CLAIM_CHILD set. +func TestHostPortClaimKeepsTwoProcessesApart(t *testing.T) { + if os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD") != "" { + p, _ := strconv.Atoi(os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD")) + if claimHostPort(p) { + os.Exit(0) + } + os.Exit(3) + } + dir := t.TempDir() + t.Setenv("SWBLOCK_TEST_PORT_CLAIMS", dir) + const p = 15151 + child := func() int { + cmd := exec.Command(os.Args[0], "-test.run", "^TestHostPortClaimKeepsTwoProcessesApart$") + cmd.Env = append(os.Environ(), "SWBLOCK_TEST_PORT_CLAIM_CHILD="+strconv.Itoa(p), "SWBLOCK_TEST_PORT_CLAIMS="+dir) + err := cmd.Run() + if ee, ok := err.(*exec.ExitError); ok { + return ee.ExitCode() + } + if err != nil { + t.Fatal(err) + } + return 0 + } + if code := child(); code != 0 { + t.Fatalf("a free port was refused to a lone process (exit %d)", code) + } + if !claimHostPort(p) { + t.Fatal("the port was not free after the first process exited") + } + if code := child(); code != 3 { + t.Fatalf("a second process took a port this one holds (exit %d)", code) + } + if claimHostPort(p) { + t.Fatal("this process handed out the same port twice") + } +} diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go new file mode 100644 index 0000000000..e3440f25eb --- /dev/null +++ b/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go @@ -0,0 +1,68 @@ +//go:build unix + +package main_test + +import ( + "fmt" + "os" + "path/filepath" + "sync" + "syscall" +) + +// hostPortClaims holds, for this test process's life, an exclusive flock on +// one file per port it handed out. Two test processes on one host (two CI +// jobs on one machine, each with its own shards) then never hand out the +// same port: the per-shard slice keeps shards of one job apart, the claim +// keeps jobs apart. The kernel drops a claim when its process exits. +var hostPortClaims struct { + sync.Mutex + files map[int]*os.File +} + +// testPortClaimDir is the host-wide directory of port claims: +// SWBLOCK_TEST_PORT_CLAIMS, else /tmp/swblock-test-port-claims (not TMPDIR, +// which differs per shard and per job). +func testPortClaimDir() string { + if d := os.Getenv("SWBLOCK_TEST_PORT_CLAIMS"); d != "" { + return d + } + return "/tmp/swblock-test-port-claims" +} + +// claimHostPort takes port p for this process, host-wide; false when +// another process holds it (or the claim cannot be made). +func claimHostPort(p int) bool { + hostPortClaims.Lock() + defer hostPortClaims.Unlock() + if hostPortClaims.files == nil { + hostPortClaims.files = map[int]*os.File{} + } + if _, ok := hostPortClaims.files[p]; ok { + return false // already handed out by this process + } + f, err := flockPortFile(testPortClaimDir(), p) + if err != nil { + return false + } + hostPortClaims.files[p] = f + return true +} + +// flockPortFile opens dir/

.lock and takes an exclusive, non-blocking +// flock on it; the open file holds the claim. +func flockPortFile(dir string, p int) (*os.File, error) { + if err := os.MkdirAll(dir, 0o777); err != nil { + return nil, err + } + _ = os.Chmod(dir, 0o777) // shared by every runner user on the host + f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%d.lock", p)), os.O_RDWR|os.O_CREATE, 0o666) + if err != nil { + return nil, err + } + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { + f.Close() + return nil, err + } + return f, nil +} diff --git a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go index 18c1a72693..c603903c5a 100644 --- a/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go +++ b/seaweed-block/cmd/blockvolume/workspace_weed_integration_test.go @@ -66,6 +66,9 @@ func w4FreePortPair(t *testing.T, used map[int]bool) int { if (p >= lo && p <= hi) || (p+10000 >= lo && p+10000 <= hi) { continue } + if !claimHostPort(p) || !claimHostPort(p+10000) { + continue + } ok := true for _, q := range []int{p, p + 10000} { l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", testListenHost(), q)) From d799063744b615cc51de22149e37202e9301efea Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 13:11:21 -0800 Subject: [PATCH 2/3] test(blockvolume): port claims open their lock files read-only; only "held" refuses A lock file created by one runner user (0644 under umask 022, never removed) was opened read-write by the next user and refused with EACCES, locking its port out for good. flock needs no write access: open read-only, create 0444. Only EWOULDBLOCK means another process holds the port; any other claim error is reported once, with its path, and the port is left to the listen probe. Co-Authored-By: Claude Opus 5.5 --- .../cmd/blockvolume/test_port_claim_test.go | 28 +++++++++++++++ .../blockvolume/test_port_claim_unix_test.go | 35 +++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_test.go index 14ba97e341..6686fdf92c 100644 --- a/seaweed-block/cmd/blockvolume/test_port_claim_test.go +++ b/seaweed-block/cmd/blockvolume/test_port_claim_test.go @@ -48,3 +48,31 @@ func TestHostPortClaimKeepsTwoProcessesApart(t *testing.T) { t.Fatal("this process handed out the same port twice") } } + +// A lock file left by another runner user, readable but not writable (0444), +// is still claimable: flock needs no write access. Held, it still keeps a +// second process out. +func TestHostPortClaimOnAReadOnlyLockFile(t *testing.T) { + if os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD") != "" { + t.Skip("child mode belongs to TestHostPortClaimKeepsTwoProcessesApart") + } + dir := t.TempDir() + t.Setenv("SWBLOCK_TEST_PORT_CLAIMS", dir) + const p = 15252 + if err := os.WriteFile(dir+"/"+strconv.Itoa(p)+".lock", nil, 0o444); err != nil { + t.Fatal(err) + } + if err := os.Chmod(dir+"/"+strconv.Itoa(p)+".lock", 0o444); err != nil { + t.Fatal(err) + } + if !claimHostPort(p) { + t.Fatal("a read-only lock file left by another user locks its port out") + } + cmd := exec.Command(os.Args[0], "-test.run", "^TestHostPortClaimKeepsTwoProcessesApart$") + cmd.Env = append(os.Environ(), "SWBLOCK_TEST_PORT_CLAIM_CHILD="+strconv.Itoa(p), "SWBLOCK_TEST_PORT_CLAIMS="+dir) + err := cmd.Run() + ee, ok := err.(*exec.ExitError) + if !ok || ee.ExitCode() != 3 { + t.Fatalf("a second process took a port held through a read-only lock file: %v", err) + } +} diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go index e3440f25eb..e4aceaa903 100644 --- a/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go +++ b/seaweed-block/cmd/blockvolume/test_port_claim_unix_test.go @@ -3,6 +3,7 @@ package main_test import ( + "errors" "fmt" "os" "path/filepath" @@ -17,7 +18,8 @@ import ( // keeps jobs apart. The kernel drops a claim when its process exits. var hostPortClaims struct { sync.Mutex - files map[int]*os.File + files map[int]*os.File + reported bool // a claim error other than "held" was reported once } // testPortClaimDir is the host-wide directory of port claims: @@ -30,8 +32,10 @@ func testPortClaimDir() string { return "/tmp/swblock-test-port-claims" } -// claimHostPort takes port p for this process, host-wide; false when -// another process holds it (or the claim cannot be made). +// claimHostPort takes port p for this process, host-wide; false when another +// process holds it. Only "would block" means held: any other error (the +// directory or file cannot be opened) is reported once, with its path, and +// the port is left to the caller's listen probe, as without claims. func claimHostPort(p int) bool { hostPortClaims.Lock() defer hostPortClaims.Unlock() @@ -42,23 +46,34 @@ func claimHostPort(p int) bool { return false // already handed out by this process } f, err := flockPortFile(testPortClaimDir(), p) - if err != nil { + switch { + case err == nil: + hostPortClaims.files[p] = f + return true + case errors.Is(err, syscall.EWOULDBLOCK): return false + default: + if !hostPortClaims.reported { + hostPortClaims.reported = true + fmt.Fprintf(os.Stderr, "test port claims off: %v\n", err) + } + return true } - hostPortClaims.files[p] = f - return true } -// flockPortFile opens dir/

.lock and takes an exclusive, non-blocking -// flock on it; the open file holds the claim. +// flockPortFile opens dir/

.lock read-only (creating it if needed) and +// takes an exclusive, non-blocking flock on it; the open file holds the +// claim. flock needs no write access, so a lock file another runner user +// created stays claimable. func flockPortFile(dir string, p int) (*os.File, error) { if err := os.MkdirAll(dir, 0o777); err != nil { return nil, err } _ = os.Chmod(dir, 0o777) // shared by every runner user on the host - f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%d.lock", p)), os.O_RDWR|os.O_CREATE, 0o666) + name := filepath.Join(dir, fmt.Sprintf("%d.lock", p)) + f, err := os.OpenFile(name, os.O_RDONLY|os.O_CREATE, 0o444) if err != nil { - return nil, err + return nil, fmt.Errorf("%s: %w", name, err) } if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { f.Close() From 80e0dc5b5f509630f126f57c977252095189bd43 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 13:12:39 -0800 Subject: [PATCH 3/3] test(blockvolume): port-claim tests use a holder and a contender subprocess The parent no longer claims, so its claim map stays empty and -count=2 passes; a hold helper keeps the claim until its stdin closes, a try helper reports taken or refused, and both run under a local deadline and are reaped. Held: refused to another process; after the holder exits: taken. Co-Authored-By: Claude Opus 5.5 --- .../cmd/blockvolume/test_port_claim_test.go | 158 +++++++++++++----- 1 file changed, 112 insertions(+), 46 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/test_port_claim_test.go b/seaweed-block/cmd/blockvolume/test_port_claim_test.go index 6686fdf92c..8d2e1c93e1 100644 --- a/seaweed-block/cmd/blockvolume/test_port_claim_test.go +++ b/seaweed-block/cmd/blockvolume/test_port_claim_test.go @@ -3,49 +3,123 @@ package main_test import ( + "bufio" + "context" "os" "os/exec" + "path/filepath" "strconv" "testing" + "time" ) -// A port claimed by one process is refused to another on the same host, and -// free again once the first exits. The other process is this test binary -// started again with SWBLOCK_TEST_PORT_CLAIM_CHILD set. -func TestHostPortClaimKeepsTwoProcessesApart(t *testing.T) { - if os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD") != "" { - p, _ := strconv.Atoi(os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD")) - if claimHostPort(p) { +// claimChild runs this test binary again as a claim helper: mode "hold" +// claims the port, prints "held" (or "refused"), and keeps the claim until +// its stdin closes; mode "try" claims and exits 0 (taken) or 3 (refused). +// The parent never claims, so its own claim map stays empty across -count. +const claimChildEnv = "SWBLOCK_TEST_PORT_CLAIM_CHILD" + +func TestMain(m *testing.M) { + if mode := os.Getenv(claimChildEnv); mode != "" { + p, _ := strconv.Atoi(os.Getenv("SWBLOCK_TEST_PORT_CLAIM_PORT")) + switch mode { + case "hold": + if !claimHostPort(p) { + os.Stdout.WriteString("refused\n") + os.Exit(3) + } + os.Stdout.WriteString("held\n") + _, _ = bufio.NewReader(os.Stdin).ReadString('\n') // until the parent closes stdin os.Exit(0) + case "try": + if claimHostPort(p) { + os.Exit(0) + } + os.Exit(3) } - os.Exit(3) + os.Exit(2) } + os.Exit(m.Run()) +} + +// claimEnv is the environment of a claim helper for port p in dir. +func claimEnv(mode, dir string, p int) []string { + return append(os.Environ(), claimChildEnv+"="+mode, "SWBLOCK_TEST_PORT_CLAIM_PORT="+strconv.Itoa(p), "SWBLOCK_TEST_PORT_CLAIMS="+dir) +} + +// tryClaim runs a "try" helper with a local deadline and returns its exit +// code. +func tryClaim(t *testing.T, dir string, p int) int { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, os.Args[0]) + cmd.Env = claimEnv("try", dir, p) + err := cmd.Run() + if ctx.Err() != nil { + t.Fatalf("the claim helper did not finish: %v", ctx.Err()) + } + if ee, ok := err.(*exec.ExitError); ok { + return ee.ExitCode() + } + if err != nil { + t.Fatal(err) + } + return 0 +} + +// holdClaim starts a "hold" helper and waits until it holds the port; the +// returned release closes its stdin and reaps it, within a deadline. +func holdClaim(t *testing.T, dir string, p int) (release func()) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + cmd := exec.CommandContext(ctx, os.Args[0]) + cmd.Env = claimEnv("hold", dir, p) + in, err := cmd.StdinPipe() + if err != nil { + cancel() + t.Fatal(err) + } + out, err := cmd.StdoutPipe() + if err != nil { + cancel() + t.Fatal(err) + } + if err := cmd.Start(); err != nil { + cancel() + t.Fatal(err) + } + released := false + release = func() { + if released { + return + } + released = true + in.Close() + _ = cmd.Wait() // bounded by ctx: a hung helper is killed at its deadline + cancel() + } + t.Cleanup(release) + line, err := bufio.NewReader(out).ReadString('\n') + if err != nil || line != "held\n" { + release() + t.Fatalf("the holder did not take the port: %q %v", line, err) + } + return release +} + +// A port held by one process is refused to another on the same host, and +// taken once the holder has exited. +func TestHostPortClaimKeepsTwoProcessesApart(t *testing.T) { dir := t.TempDir() - t.Setenv("SWBLOCK_TEST_PORT_CLAIMS", dir) const p = 15151 - child := func() int { - cmd := exec.Command(os.Args[0], "-test.run", "^TestHostPortClaimKeepsTwoProcessesApart$") - cmd.Env = append(os.Environ(), "SWBLOCK_TEST_PORT_CLAIM_CHILD="+strconv.Itoa(p), "SWBLOCK_TEST_PORT_CLAIMS="+dir) - err := cmd.Run() - if ee, ok := err.(*exec.ExitError); ok { - return ee.ExitCode() - } - if err != nil { - t.Fatal(err) - } - return 0 + release := holdClaim(t, dir, p) + if code := tryClaim(t, dir, p); code != 3 { + t.Fatalf("a second process took a held port (exit %d)", code) } - if code := child(); code != 0 { - t.Fatalf("a free port was refused to a lone process (exit %d)", code) - } - if !claimHostPort(p) { - t.Fatal("the port was not free after the first process exited") - } - if code := child(); code != 3 { - t.Fatalf("a second process took a port this one holds (exit %d)", code) - } - if claimHostPort(p) { - t.Fatal("this process handed out the same port twice") + release() + if code := tryClaim(t, dir, p); code != 0 { + t.Fatalf("the port was not free after its holder exited (exit %d)", code) } } @@ -53,26 +127,18 @@ func TestHostPortClaimKeepsTwoProcessesApart(t *testing.T) { // is still claimable: flock needs no write access. Held, it still keeps a // second process out. func TestHostPortClaimOnAReadOnlyLockFile(t *testing.T) { - if os.Getenv("SWBLOCK_TEST_PORT_CLAIM_CHILD") != "" { - t.Skip("child mode belongs to TestHostPortClaimKeepsTwoProcessesApart") - } dir := t.TempDir() - t.Setenv("SWBLOCK_TEST_PORT_CLAIMS", dir) const p = 15252 - if err := os.WriteFile(dir+"/"+strconv.Itoa(p)+".lock", nil, 0o444); err != nil { + name := filepath.Join(dir, strconv.Itoa(p)+".lock") + if err := os.WriteFile(name, nil, 0o444); err != nil { t.Fatal(err) } - if err := os.Chmod(dir+"/"+strconv.Itoa(p)+".lock", 0o444); err != nil { + if err := os.Chmod(name, 0o444); err != nil { t.Fatal(err) } - if !claimHostPort(p) { - t.Fatal("a read-only lock file left by another user locks its port out") - } - cmd := exec.Command(os.Args[0], "-test.run", "^TestHostPortClaimKeepsTwoProcessesApart$") - cmd.Env = append(os.Environ(), "SWBLOCK_TEST_PORT_CLAIM_CHILD="+strconv.Itoa(p), "SWBLOCK_TEST_PORT_CLAIMS="+dir) - err := cmd.Run() - ee, ok := err.(*exec.ExitError) - if !ok || ee.ExitCode() != 3 { - t.Fatalf("a second process took a port held through a read-only lock file: %v", err) + release := holdClaim(t, dir, p) + if code := tryClaim(t, dir, p); code != 3 { + t.Fatalf("a second process took a port held through a read-only lock file (exit %d)", code) } + release() }