From 94bcc14a82cf064d9981c5c62c90ccf41b3674d4 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 19:22:12 +0100 Subject: [PATCH] zfs: the keeper's socket test no longer races, and a refused peer is told so The test switched the peer's uid in a plain variable the keeper's serving goroutines read, a data race that fails it under -race; it is an atomic now. A peer the keeper refuses could also get "broken pipe" instead of the refusal, when the keeper closed before the request was written: the client now reads the refusal that waits on the socket. A request larger than the socket's buffer makes that case happen every time. Builds: A-37 (only root opens the keeper's socket; a non-root peer is told so); design 47 H section 10 rows 165 and 167 (hq branch design/47-H1-answers-59, 81fa1ee4) --- modules/zfs/cmd/zfs-tools/keeper.go | 5 +++++ modules/zfs/cmd/zfs-tools/keeper_test.go | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/zfs/cmd/zfs-tools/keeper.go b/modules/zfs/cmd/zfs-tools/keeper.go index d4be817d1..399487826 100644 --- a/modules/zfs/cmd/zfs-tools/keeper.go +++ b/modules/zfs/cmd/zfs-tools/keeper.go @@ -328,6 +328,11 @@ func (c keeperClient) exchange(r KeeperRequest) (a KeeperAnswer, err error, unan defer conn.Close() _ = conn.SetDeadline(time.Now().Add(KeeperTimeout)) if err := json.NewEncoder(conn).Encode(r); err != nil { + // A keeper that refuses the peer answers and closes without reading, so the write can meet a closed socket + // (broken pipe) while the refusal already waits to be read: the refusal is the answer (issue 1960). + if derr := json.NewDecoder(conn).Decode(&a); derr == nil && a.Error != "" { + return a, errors.New(a.Error), false + } return KeeperAnswer{}, err, true } if err := json.NewDecoder(conn).Decode(&a); err != nil { diff --git a/modules/zfs/cmd/zfs-tools/keeper_test.go b/modules/zfs/cmd/zfs-tools/keeper_test.go index 6b6508917..5d36d1d39 100644 --- a/modules/zfs/cmd/zfs-tools/keeper_test.go +++ b/modules/zfs/cmd/zfs-tools/keeper_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strings" + "sync/atomic" "testing" ) @@ -91,10 +92,12 @@ func shortTempDir(prefix string) (string, error) { // keeper answering it acts as with the bus down, with a warning (A-8). func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing.T) { st := memStates{} - uid := 0 + // The peer's uid is read by the keeper's serving goroutines and switched by the test: an atomic, so no connection + // sees it half-written (issue 1960: a data race failed the test under -race). + var uid atomic.Int32 k := &Keeper{Emit: func(string, any) error { return nil }, States: st, Call: func(string, any) (json.RawMessage, error) { return nil, errors.New("down") }, - PeerOf: func(net.Conn) (int, error) { return uid, nil }} + PeerOf: func(net.Conn) (int, error) { return int(uid.Load()), nil }} dir, err := shortTempDir("zk") if err != nil { t.Fatal(err) @@ -121,13 +124,21 @@ func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing. if _, err := c.call(ToolSchedule, map[string]any{}); err == nil || !strings.Contains(err.Error(), "down") { t.Errorf("an invoke that failed: %v", err) } - uid = 1000 + uid.Store(1000) if err := c.Put(StateSchedule, "x", 1); err == nil || !strings.Contains(err.Error(), "only root") { t.Errorf("a peer that is not root: %v", err) } + // A request larger than the socket's buffer: the keeper refuses and closes before it is written whole, so the write + // meets a closed socket every time; the peer is still told it is not root (issue 1960). + if err := c.Put(StateSchedule, "y", strings.Repeat("x", 1<<20)); err == nil || !strings.Contains(err.Error(), "only root") { + t.Errorf("a peer that is not root, its write cut: %v", err) + } if _, left := st["schedule/x"]; left { t.Errorf("a refused peer's call was read") } + if _, left := st["schedule/y"]; left { + t.Errorf("a refused peer's cut call was read") + } // No socket variable: the machine keeps no bus and says so. t.Setenv(KeeperSocketEnv, "") m := newFake(t).machine() @@ -151,6 +162,8 @@ func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing. if err != nil { t.Fatal(err) } + // The socket is bound before the call below, so the call waits in its backlog until Serve accepts it: the keeper + // is up from the moment Listen returns, whenever its goroutine runs. lctx, lcancel := context.WithCancel(context.Background()) defer lcancel() go (&Keeper{Emit: func(string, any) error { return nil }, States: memStates{}, PeerOf: func(net.Conn) (int, error) { return 0, nil }, @@ -159,7 +172,7 @@ func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing. t.Errorf("a keeper that came up later is not used: %v", err) } t.Setenv(KeeperSocketEnv, sock) - uid = 0 + uid.Store(0) m2 := newFake(t).machine() m2.JoinBus() if m2.Emit == nil || m2.States == nil || m2.Call == nil || m2.keeper == nil {