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)
This commit is contained in:
2026-11-21 19:26:14 +01:00
parent 461868b9fe
commit 94bcc14a82
2 changed files with 22 additions and 4 deletions
+5
View File
@@ -328,6 +328,11 @@ func (c keeperClient) exchange(r KeeperRequest) (a KeeperAnswer, err error, unan
defer conn.Close() defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(KeeperTimeout)) _ = conn.SetDeadline(time.Now().Add(KeeperTimeout))
if err := json.NewEncoder(conn).Encode(r); err != nil { 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 return KeeperAnswer{}, err, true
} }
if err := json.NewDecoder(conn).Decode(&a); err != nil { if err := json.NewDecoder(conn).Decode(&a); err != nil {
+17 -4
View File
@@ -8,6 +8,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync/atomic"
"testing" "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). // keeper answering it acts as with the bus down, with a warning (A-8).
func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing.T) { func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing.T) {
st := memStates{} 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, k := &Keeper{Emit: func(string, any) error { return nil }, States: st,
Call: func(string, any) (json.RawMessage, error) { return nil, errors.New("down") }, 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") dir, err := shortTempDir("zk")
if err != nil { if err != nil {
t.Fatal(err) 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") { if _, err := c.call(ToolSchedule, map[string]any{}); err == nil || !strings.Contains(err.Error(), "down") {
t.Errorf("an invoke that failed: %v", err) 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") { 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) 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 { if _, left := st["schedule/x"]; left {
t.Errorf("a refused peer's call was read") 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. // No socket variable: the machine keeps no bus and says so.
t.Setenv(KeeperSocketEnv, "") t.Setenv(KeeperSocketEnv, "")
m := newFake(t).machine() m := newFake(t).machine()
@@ -151,6 +162,8 @@ func TestOnlyRootOpensTheKeepersSocketAndANeedlessKeeperIsTheBusDown(t *testing.
if err != nil { if err != nil {
t.Fatal(err) 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()) lctx, lcancel := context.WithCancel(context.Background())
defer lcancel() defer lcancel()
go (&Keeper{Emit: func(string, any) error { return nil }, States: memStates{}, PeerOf: func(net.Conn) (int, error) { return 0, nil }, 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.Errorf("a keeper that came up later is not used: %v", err)
} }
t.Setenv(KeeperSocketEnv, sock) t.Setenv(KeeperSocketEnv, sock)
uid = 0 uid.Store(0)
m2 := newFake(t).machine() m2 := newFake(t).machine()
m2.JoinBus() m2.JoinBus()
if m2.Emit == nil || m2.States == nil || m2.Call == nil || m2.keeper == nil { if m2.Emit == nil || m2.States == nil || m2.Call == nil || m2.keeper == nil {