mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
50bc8fe6ba
commit
d799063744
2 files changed
+53
-10
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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/<p>.lock and takes an exclusive, non-blocking
|
||||
// flock on it; the open file holds the claim.
|
||||
// flockPortFile opens dir/<p>.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()
|
||||
|
||||
Reference in new issue
Block a user