From a7f52cf23b80147d51c1964926fa6107431f7fd9 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Sun, 1 Nov 2026 22:20:54 +0100 Subject: [PATCH 1/8] driver/memcached: tests for the per-operation Config.Timeout (RED, Refs #1218) --- driver/memcached/op_timeout_1218_test.go | 244 +++++++++++++++++++++++ driver/memcached/options.go | 6 + 2 files changed, 250 insertions(+) create mode 100644 driver/memcached/op_timeout_1218_test.go diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go new file mode 100644 index 000000000..ff356d8d2 --- /dev/null +++ b/driver/memcached/op_timeout_1218_test.go @@ -0,0 +1,244 @@ +package memcached + +// celeris#1218: Config.Timeout, documented as the per-operation default (3 s), was +// never applied. A command on a server that accepts TCP and never answers it held +// the caller until its context ended: for ever under context.Background(). + +import ( + "bytes" + "context" + "encoding/binary" + "errors" + "io" + "net" + "strings" + "sync" + "testing" + "time" + + "github.com/goceleris/celeris/driver/internal/async" + "github.com/goceleris/celeris/driver/internal/eventloop" + "github.com/goceleris/celeris/internal/driver/memcached/protocol" +) + +// startMuteServer accepts TCP conns and reads what they send. For the binary +// protocol it answers the first request of a conn (the VERSION handshake) and +// nothing after it; for the text protocol it never answers. It counts the conns +// the client closed. +func startMuteServer(t *testing.T, binaryProto bool) (addr string, closed *int32Counter) { + t.Helper() + closed = &int32Counter{} + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var mu sync.Mutex + var conns []net.Conn + t.Cleanup(func() { + _ = ln.Close() + mu.Lock() + defer mu.Unlock() + for _, c := range conns { + _ = c.Close() + } + }) + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + mu.Lock() + conns = append(conns, c) + mu.Unlock() + go func() { + defer closed.add() + if binaryProto { + var hdr [protocol.BinHeaderLen]byte + if _, err := io.ReadFull(c, hdr[:]); err != nil { + return + } + body := make([]byte, binary.BigEndian.Uint32(hdr[8:12])) + if _, err := io.ReadFull(c, body); err != nil { + return + } + var out bytes.Buffer + writeResp(&out, hdr[1], 0, 0, binary.BigEndian.Uint32(hdr[12:16]), nil, nil, []byte("1.6.0")) + if _, err := c.Write(out.Bytes()); err != nil { + return + } + } + _, _ = io.Copy(io.Discard, c) + }() + } + }() + return ln.Addr().String(), closed +} + +type int32Counter struct { + mu sync.Mutex + n int +} + +func (c *int32Counter) add() { c.mu.Lock(); c.n++; c.mu.Unlock() } +func (c *int32Counter) get() int { + c.mu.Lock() + defer c.mu.Unlock() + return c.n +} + +// opClients are the three ways a Client reaches its conns: direct (what a client +// without an engine uses), the unix event loop, and the net.Conn loop. +var opClients = []struct { + name string + new func(t *testing.T, cfg Config) *Client +}{ + {"direct", func(t *testing.T, cfg Config) *Client { + c := &Client{cfg: cfg, pool: newDirectPool(cfg)} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, + {"eventloop", func(t *testing.T, cfg Config) *Client { + prov, err := eventloop.Resolve(nil) + if err != nil { + t.Fatal(err) + } + c := &Client{cfg: cfg, pool: newPool(cfg, prov, true)} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, + {"netconn", func(t *testing.T, cfg Config) *Client { + prov := netConnProvider(t) + p := &memcachedPool{cfg: cfg, provider: prov} + p.pool = async.NewPool[*memcachedConn](async.PoolConfig{ + MaxOpen: 4, MaxIdlePerWorker: 2, MaxLifetime: time.Hour, MaxIdleTime: time.Hour, NumWorkers: 2, + }, func(ctx context.Context, w int) (*memcachedConn, error) { + return dialMemcachedConnNetConn(ctx, prov, cfg, w) + }) + c := &Client{cfg: cfg, pool: p} + t.Cleanup(func() { _ = c.Close() }) + return c + }}, +} + +// opWithin runs op and fails the test (not the test binary's timeout) if it is +// still blocked after limit. +func opWithin(t *testing.T, limit time.Duration, ctx context.Context, op func(ctx context.Context) error) (time.Duration, error) { + t.Helper() + done := make(chan error, 1) + start := time.Now() + go func() { done <- op(ctx) }() + select { + case err := <-done: + return time.Since(start), err + case <-time.After(limit): + t.Fatalf("the operation is still blocked after %v: Config.Timeout is not applied", limit) + return 0, nil + } +} + +// A command on a server that never answers: it fails after about Config.Timeout, +// with a context that has no deadline and with one that is much later, for the text +// and the binary protocol, on the three paths, for a command (Get) and for Ping and +// Version. The conn it used is dropped from the pool. +func TestOperationIsBoundedByTimeout1218(t *testing.T) { + const bound = 300 * time.Millisecond + for _, proto := range []Protocol{ProtocolText, ProtocolBinary} { + for _, cl := range opClients { + for _, op := range []string{"Get", "Ping", "Version"} { + for _, cx := range []string{"background", "later-deadline"} { + t.Run(proto.String()+"/"+cl.name+"/"+op+"/"+cx, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, proto == ProtocolBinary) + c := cl.new(t, Config{Addr: addr, Protocol: proto, Timeout: bound, DialTimeout: 5 * time.Second}) + ctx := context.Background() + if cx == "later-deadline" { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, 30*time.Second) + defer cancel() + } + elapsed, err := opWithin(t, 2*time.Second, ctx, func(ctx context.Context) error { + switch op { + case "Get": + _, err := c.Get(ctx, "k") + return err + case "Ping": + return c.Ping(ctx) + } + _, err := c.Version(ctx) + return err + }) + t.Logf("%s: %v after %v", t.Name(), err, elapsed.Round(10*time.Millisecond)) + if err == nil { + t.Fatal("no error") + } + if !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("error %v does not match context.DeadlineExceeded", err) + } + if !strings.Contains(err.Error(), "timed out") { + t.Errorf("error %q does not say that the operation timed out", err) + } + if elapsed < bound/2 { + t.Errorf("returned after %v, before the bound of %v", elapsed, bound) + } + if n := c.PoolStats().Open; n != 0 { + t.Errorf("the pool holds %d conns after the timeout, want 0 (the conn is poisoned)", n) + } + }) + } + } + } + } +} + +// A caller's shorter deadline still wins, and its error is not relabelled as the +// driver's. (Passes on main by design: it guards the "no earlier one" rule.) +func TestOperationKeepsTheCallersShorterDeadline1218(t *testing.T) { + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: 30 * time.Second}) + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + defer cancel() + elapsed, err := opWithin(t, 2*time.Second, ctx, func(ctx context.Context) error { + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.DeadlineExceeded) && !isTimeout(err) { + t.Fatalf("error %v, want a deadline error", err) + } + if strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %q: the caller's own deadline is reported as Config.Timeout", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the caller's 200 ms", elapsed) + } + }) + } +} + +func isTimeout(err error) bool { + var ne net.Error + return errors.As(err, &ne) && ne.Timeout() +} + +// A client that answers within the bound is not affected, and the bound does not +// stay on the conn: the conn is used again after the time it would have fired. +// (Passes on main by design.) +func TestTimeoutDoesNotTouchAnAnsweredOperation1218(t *testing.T) { + f := startFake(t) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: 200 * time.Millisecond}) + ctx := context.Background() + if err := c.Set(ctx, "k"+cl.name, "v", 0); err != nil { + t.Fatal(err) + } + time.Sleep(300 * time.Millisecond) // past Timeout + if v, err := c.Get(ctx, "k"+cl.name); err != nil || v != "v" { + t.Fatalf("Get after Timeout passed = %q, %v", v, err) + } + }) + } +} diff --git a/driver/memcached/options.go b/driver/memcached/options.go index b41f1141b..e6519cae9 100644 --- a/driver/memcached/options.go +++ b/driver/memcached/options.go @@ -1,9 +1,15 @@ package memcached import ( + "sync/atomic" "time" ) +// testHookDefaultOpTimeout, when set, replaces the default per-operation timeout +// (defaultTimeout, 3 s) of a client whose Config.Timeout is unset. A test sets it +// to avoid waiting 3 s, and resets it to nil when it is done. +var testHookDefaultOpTimeout atomic.Pointer[time.Duration] + // Protocol selects the memcached wire dialect spoken by a Client. Every // memcached deployment supports text; binary was deprecated upstream but // remains broadly available and is sometimes preferred for its CAS semantics From 40f9632e78b5171f75b872e7e124dd011c5be672 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Sun, 1 Nov 2026 22:24:26 +0100 Subject: [PATCH 2/8] driver/memcached: apply Config.Timeout as the per-operation deadline (Refs #1218) --- driver/memcached/cluster.go | 3 +- driver/memcached/commands.go | 63 ++++++++++++-- driver/memcached/op_timeout_1218_test.go | 101 +++++++++++++++++++++-- driver/memcached/options.go | 14 +++- 4 files changed, 163 insertions(+), 18 deletions(-) diff --git a/driver/memcached/cluster.go b/driver/memcached/cluster.go index 12af1430a..3e46649b1 100644 --- a/driver/memcached/cluster.go +++ b/driver/memcached/cluster.go @@ -49,7 +49,8 @@ type ClusterConfig struct { // [Config.MaxOpen]; zero means NumWorkers * 4 per node. MaxOpen int - // Timeout is the advisory per-op deadline forwarded to each node client. + // Timeout is the per-operation deadline forwarded to each node client: see + // [Config.Timeout]. Timeout time.Duration // DialTimeout is forwarded to each node client: see [Config.DialTimeout] diff --git a/driver/memcached/commands.go b/driver/memcached/commands.go index 9caec7267..ee70973bb 100644 --- a/driver/memcached/commands.go +++ b/driver/memcached/commands.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math" + "os" "strconv" "time" @@ -107,15 +108,65 @@ func asBytes(v any) ([]byte, error) { } } -// run acquires a conn, runs fn with it, and releases the conn. On ctx -// cancellation or context deadline the conn is discarded because the server -// may still send a stale reply. +// opTimeout is the per-operation bound of the client: Config.Timeout, the default +// (3 s) when it is zero, none when it is negative. +func (c *Client) opTimeout() time.Duration { + t := c.cfg.Timeout + if t == 0 { + t = defaultTimeout + if h := testHookDefaultOpTimeout.Load(); h != nil { + t = *h + } + } + return max(t, 0) +} + +// opContext limits ctx by the per-operation timeout (celeris#1218): the returned +// context ends at the caller's deadline or after the timeout, whichever is first. A +// caller's deadline that is not later than the timeout is left alone (ctx comes back +// as it is, and its error is the caller's). timeout is 0 when ctx was not limited. +func (c *Client) opContext(ctx context.Context) (octx context.Context, cancel context.CancelFunc, timeout time.Duration) { + t := c.opTimeout() + if t == 0 { + return ctx, func() {}, 0 + } + if dl, ok := ctx.Deadline(); ok && !dl.After(time.Now().Add(t)) { + return ctx, func() {}, 0 + } + octx, cancel = context.WithTimeout(ctx, t) + return octx, cancel, t +} + +// opError reports an error of an operation run on a context from opContext. When +// the per-operation timeout ended it (timeout > 0, the caller's context is alive, +// and either the derived context expired or a direct conn's socket deadline fired a +// hair before it), the error says so and matches context.DeadlineExceeded, which +// also tells run to drop the conn. +func opError(ctx, octx context.Context, err error, timeout time.Duration) error { + if err == nil || timeout == 0 || ctx.Err() != nil { + return err + } + if octx.Err() == nil && !errors.Is(err, os.ErrDeadlineExceeded) { + return err + } + if errors.Is(err, context.DeadlineExceeded) { + return fmt.Errorf("celeris-memcached: operation timed out after %v: %w", timeout, err) + } + return fmt.Errorf("celeris-memcached: operation timed out after %v: %w: %w", timeout, context.DeadlineExceeded, err) +} + +// run acquires a conn, runs fn with it, and releases the conn. fn runs under the +// per-operation timeout (opContext); acquiring the conn does not (a new conn has +// its own DialTimeout). On ctx cancellation or context deadline the conn is +// discarded because the server may still send a stale reply. func (c *Client) run(ctx context.Context, fn func(ctx context.Context, conn *memcachedConn) error) error { conn, err := c.pool.acquire(ctx, workerFromCtx(ctx)) if err != nil { return err } - ferr := fn(ctx, conn) + octx, cancel, timeout := c.opContext(ctx) + ferr := opError(ctx, octx, fn(octx, conn), timeout) + cancel() if ferr != nil && (errors.Is(ferr, context.Canceled) || errors.Is(ferr, context.DeadlineExceeded)) { c.pool.discard(conn) return ferr @@ -851,7 +902,9 @@ func (c *Client) Ping(ctx context.Context) error { if err != nil { return err } - perr := conn.Ping(ctx) + octx, cancel, timeout := c.opContext(ctx) + perr := opError(ctx, octx, conn.Ping(octx), timeout) + cancel() if perr != nil { if errors.Is(perr, context.Canceled) || errors.Is(perr, context.DeadlineExceeded) { c.pool.discard(conn) diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go index ff356d8d2..94b4c431b 100644 --- a/driver/memcached/op_timeout_1218_test.go +++ b/driver/memcached/op_timeout_1218_test.go @@ -81,11 +81,6 @@ type int32Counter struct { } func (c *int32Counter) add() { c.mu.Lock(); c.n++; c.mu.Unlock() } -func (c *int32Counter) get() int { - c.mu.Lock() - defer c.mu.Unlock() - return c.n -} // opClients are the three ways a Client reaches its conns: direct (what a client // without an engine uses), the unix event loop, and the net.Conn loop. @@ -123,11 +118,11 @@ var opClients = []struct { // opWithin runs op and fails the test (not the test binary's timeout) if it is // still blocked after limit. -func opWithin(t *testing.T, limit time.Duration, ctx context.Context, op func(ctx context.Context) error) (time.Duration, error) { +func opWithin(t *testing.T, limit time.Duration, op func() error) (time.Duration, error) { t.Helper() done := make(chan error, 1) start := time.Now() - go func() { done <- op(ctx) }() + go func() { done <- op() }() select { case err := <-done: return time.Since(start), err @@ -157,7 +152,7 @@ func TestOperationIsBoundedByTimeout1218(t *testing.T) { ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() } - elapsed, err := opWithin(t, 2*time.Second, ctx, func(ctx context.Context) error { + elapsed, err := opWithin(t, 2*time.Second, func() error { switch op { case "Get": _, err := c.Get(ctx, "k") @@ -201,7 +196,7 @@ func TestOperationKeepsTheCallersShorterDeadline1218(t *testing.T) { c := cl.new(t, Config{Addr: addr, Timeout: 30 * time.Second}) ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) defer cancel() - elapsed, err := opWithin(t, 2*time.Second, ctx, func(ctx context.Context) error { + elapsed, err := opWithin(t, 2*time.Second, func() error { _, err := c.Get(ctx, "k") return err }) @@ -242,3 +237,91 @@ func TestTimeoutDoesNotTouchAnAnsweredOperation1218(t *testing.T) { }) } } + +// A client whose Timeout is unset gets the default (3 s). The test lowers the +// default through the test hook, so it does not wait 3 s. +func TestOperationIsBoundedByTheDefaultTimeout1218(t *testing.T) { + const def = 300 * time.Millisecond + d := def + testHookDefaultOpTimeout.Store(&d) + t.Cleanup(func() { testHookDefaultOpTimeout.Store(nil) }) + for _, proto := range []Protocol{ProtocolText, ProtocolBinary} { + for _, cl := range opClients { + t.Run(proto.String()+"/"+cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, proto == ProtocolBinary) + c := cl.new(t, Config{Addr: addr, Protocol: proto}) // no Timeout + elapsed, err := opWithin(t, 2*time.Second, func() error { + ctx := context.Background() + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.DeadlineExceeded) || !strings.Contains(err.Error(), "after "+def.String()) { + t.Errorf("error %v, want the operation timeout of the default (%v)", err, def) + } + if elapsed < def/2 { + t.Errorf("returned after %v, before the default of %v", elapsed, def) + } + }) + } + } +} + +// A negative Timeout turns the per-operation deadline off: only the caller's +// context ends the operation (here a deadline of 900 ms, three times the lowered +// default), and its error is not relabelled. +func TestNegativeTimeoutTurnsTheOperationBoundOff1218(t *testing.T) { + d := 300 * time.Millisecond + testHookDefaultOpTimeout.Store(&d) + t.Cleanup(func() { testHookDefaultOpTimeout.Store(nil) }) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: -1}) + ctx, cancel := context.WithTimeout(context.Background(), 900*time.Millisecond) + defer cancel() + elapsed, err := opWithin(t, 3*time.Second, func() error { + _, err := c.Get(ctx, "k") + return err + }) + if err == nil || strings.Contains(err.Error(), "operation timed out") || elapsed < 800*time.Millisecond { + t.Errorf("returned after %v with %v: the bound was not off", elapsed, err) + } + }) + } +} + +// A caller that cancels its context gets its own cancellation back, not relabelled +// as the driver's timeout, and the conn is dropped. (A direct conn does not watch a +// context's cancellation, only its deadline, so it is not in the cases.) +func TestOperationKeepsTheCallersCancellation1218(t *testing.T) { + if defaultTimeout != 3*time.Second { + t.Errorf("defaultTimeout = %v, want the documented 3 s", defaultTimeout) + } + for _, cl := range opClients { + if cl.name == "direct" { + continue + } + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr, _ := startMuteServer(t, false) + c := cl.new(t, Config{Addr: addr, Timeout: 30 * time.Second}) + ctx, cancel := context.WithCancel(context.Background()) + time.AfterFunc(200*time.Millisecond, cancel) + elapsed, err := opWithin(t, 2*time.Second, func() error { + _, err := c.Get(ctx, "k") + return err + }) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %v, want the caller's context.Canceled as it is", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the cancel at 200 ms", elapsed) + } + if n := c.PoolStats().Open; n != 0 { + t.Errorf("the pool holds %d conns after the cancel, want 0", n) + } + }) + } +} diff --git a/driver/memcached/options.go b/driver/memcached/options.go index e6519cae9..b7cfcd29e 100644 --- a/driver/memcached/options.go +++ b/driver/memcached/options.go @@ -63,8 +63,15 @@ type Config struct { // followed by a silent server takes up to about twice DialTimeout. A shorter // deadline of the caller's context wins. The text protocol has no handshake. DialTimeout time.Duration - // Timeout is the advisory per-op deadline used when the caller's context - // carries no deadline. Default 3s. + // Timeout is the per-operation deadline: a command (and Ping, Version, + // Stats, ...) that has not completed after Timeout fails with an error that + // matches [context.DeadlineExceeded], and the conn it used is dropped. It + // applies whether or not the caller's context has a deadline; a deadline of the + // caller's that is not later than Timeout wins and its error is returned as it + // is. It does not cover waiting for a pool slot or dialling a new conn (that is + // [Config.DialTimeout]'s). Zero means the default, 3s; a negative value turns + // the per-operation deadline off, and the caller's context is then the only + // bound. Timeout time.Duration // MaxLifetime is the max age of a pooled conn; default 30m. MaxLifetime time.Duration @@ -94,7 +101,8 @@ func WithMaxIdlePerWorker(n int) Option { return func(c *Config) { c.MaxIdlePerW // binary-protocol handshake. func WithDialTimeout(d time.Duration) Option { return func(c *Config) { c.DialTimeout = d } } -// WithTimeout sets the default per-op deadline when the caller's ctx has none. +// WithTimeout sets [Config.Timeout], the per-operation deadline (3s when unset, +// negative for none). func WithTimeout(d time.Duration) Option { return func(c *Config) { c.Timeout = d } } // WithMaxLifetime sets the pooled conn lifetime. From 38b1d2686acad321b4fc97e2c7b1a0cdd01240b8 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 19:21:56 +0100 Subject: [PATCH 3/8] driver/memcached: apply the per-operation limit without allocating (a value and a reusable timer, no context); wake a direct read on the caller's cancel (Refs #1218) --- driver/memcached/commands.go | 71 +++++++-------- driver/memcached/conn.go | 105 ++++++++++++++++++++++- driver/memcached/op_alloc_1218_test.go | 40 +++++++++ driver/memcached/op_timeout_1218_test.go | 51 ++++++++--- driver/memcached/options.go | 3 +- 5 files changed, 220 insertions(+), 50 deletions(-) create mode 100644 driver/memcached/op_alloc_1218_test.go diff --git a/driver/memcached/commands.go b/driver/memcached/commands.go index ee70973bb..81f89d3f1 100644 --- a/driver/memcached/commands.go +++ b/driver/memcached/commands.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "math" - "os" "strconv" "time" @@ -121,52 +120,56 @@ func (c *Client) opTimeout() time.Duration { return max(t, 0) } -// opContext limits ctx by the per-operation timeout (celeris#1218): the returned -// context ends at the caller's deadline or after the timeout, whichever is first. A -// caller's deadline that is not later than the timeout is left alone (ctx comes back -// as it is, and its error is the caller's). timeout is 0 when ctx was not limited. -func (c *Client) opContext(ctx context.Context) (octx context.Context, cancel context.CancelFunc, timeout time.Duration) { +// opLimit works out the per-operation limit of one command (celeris#1218), as a +// value: no context is built, so a command pays no allocation for it. It returns +// the timeout and the time it ends, or zero values when the command is not limited +// by it: the timeout is off, or the caller's deadline is not later than it (the +// caller's deadline then ends the command, and its error is the caller's). +func (c *Client) opLimit(ctx context.Context) (timeout time.Duration, deadline time.Time) { t := c.opTimeout() if t == 0 { - return ctx, func() {}, 0 + return 0, time.Time{} } - if dl, ok := ctx.Deadline(); ok && !dl.After(time.Now().Add(t)) { - return ctx, func() {}, 0 + dl := time.Now().Add(t) + if cdl, ok := ctx.Deadline(); ok && !cdl.After(dl) { + return 0, time.Time{} } - octx, cancel = context.WithTimeout(ctx, t) - return octx, cancel, t + return t, dl } -// opError reports an error of an operation run on a context from opContext. When -// the per-operation timeout ended it (timeout > 0, the caller's context is alive, -// and either the derived context expired or a direct conn's socket deadline fired a -// hair before it), the error says so and matches context.DeadlineExceeded, which -// also tells run to drop the conn. -func opError(ctx, octx context.Context, err error, timeout time.Duration) error { - if err == nil || timeout == 0 || ctx.Err() != nil { - return err - } - if octx.Err() == nil && !errors.Is(err, os.ErrDeadlineExceeded) { - return err +// beginOp puts the per-operation limit on conn for one command and, for a direct +// conn (which waits in a socket read that does not look at a context), arms the wake +// of its read on the end of a cancellable ctx. It returns the func that disarms the +// wake, nil when none was armed (a ctx that cannot be cancelled costs nothing, not +// even a closure). Pair it with endOp(disarm), after opError. +func (c *Client) beginOp(ctx context.Context, conn *memcachedConn) (disarm func()) { + conn.opTimeout, conn.opDeadline = c.opLimit(ctx) + if conn.useDirect && ctx.Done() != nil { + return conn.wakeReadOnDone(ctx) } - if errors.Is(err, context.DeadlineExceeded) { - return fmt.Errorf("celeris-memcached: operation timed out after %v: %w", timeout, err) + return nil +} + +// endOp ends the limit of the command that ran on conn and disarms the wake. +func endOp(conn *memcachedConn, disarm func()) { + if disarm != nil { + disarm() } - return fmt.Errorf("celeris-memcached: operation timed out after %v: %w: %w", timeout, context.DeadlineExceeded, err) + conn.opTimeout, conn.opDeadline = 0, time.Time{} } // run acquires a conn, runs fn with it, and releases the conn. fn runs under the -// per-operation timeout (opContext); acquiring the conn does not (a new conn has -// its own DialTimeout). On ctx cancellation or context deadline the conn is -// discarded because the server may still send a stale reply. +// per-operation timeout (beginOp); acquiring the conn does not (a new conn has its +// own DialTimeout). On ctx cancellation or context deadline the conn is discarded +// because the server may still send a stale reply. func (c *Client) run(ctx context.Context, fn func(ctx context.Context, conn *memcachedConn) error) error { conn, err := c.pool.acquire(ctx, workerFromCtx(ctx)) if err != nil { return err } - octx, cancel, timeout := c.opContext(ctx) - ferr := opError(ctx, octx, fn(octx, conn), timeout) - cancel() + disarm := c.beginOp(ctx, conn) + ferr := conn.opError(ctx, fn(ctx, conn)) + endOp(conn, disarm) if ferr != nil && (errors.Is(ferr, context.Canceled) || errors.Is(ferr, context.DeadlineExceeded)) { c.pool.discard(conn) return ferr @@ -902,9 +905,9 @@ func (c *Client) Ping(ctx context.Context) error { if err != nil { return err } - octx, cancel, timeout := c.opContext(ctx) - perr := opError(ctx, octx, conn.Ping(octx), timeout) - cancel() + disarm := c.beginOp(ctx, conn) + perr := conn.opError(ctx, conn.Ping(ctx)) + endOp(conn, disarm) if perr != nil { if errors.Is(perr, context.Canceled) || errors.Is(perr, context.DeadlineExceeded) { c.pool.discard(conn) diff --git a/driver/memcached/conn.go b/driver/memcached/conn.go index 89c541187..49d6373d6 100644 --- a/driver/memcached/conn.go +++ b/driver/memcached/conn.go @@ -61,6 +61,15 @@ type syncMultiRoundTripper interface { type memcachedConn struct { useDirect bool + // opTimeout and opDeadline are the per-operation limit of the command that is + // running on this conn (celeris#1218): both zero when it is not limited by + // Config.Timeout. They are set by Client.beginOp and cleared by endOp, on the + // goroutine that owns the conn for the command. opTimer is the timer a loop + // conn parks on for it, created on first use and reused. + opTimeout time.Duration + opDeadline time.Time + opTimer *time.Timer + // Direct-mode fields. tcp *net.TCPConn // tcpFd caches the raw fd for MSG_DONTWAIT peeks. Pre-netpoll @@ -599,13 +608,99 @@ func (c *memcachedConn) waitTransport(ctx context.Context, req *mcRequest) error default: } } + tc := c.armOpTimer() + defer c.disarmOpTimer() select { case <-req.doneCh: return nil case <-ctx.Done(): _ = c.closeWithErr(ctx.Err()) return ctx.Err() + case <-tc: + return c.opTimedOut(nil) + } +} + +// opTimeoutError is the error of a command that Config.Timeout ended. It matches +// context.DeadlineExceeded (and says "timed out"); cause is the socket's own +// timeout on a direct conn, nil on a loop conn. +type opTimeoutError struct { + d time.Duration + cause error +} + +func (e *opTimeoutError) Error() string { + s := fmt.Sprintf("celeris-memcached: operation timed out after %v: %v", e.d, context.DeadlineExceeded) + if e.cause != nil { + s += ": " + e.cause.Error() + } + return s +} +func (e *opTimeoutError) Is(target error) bool { return target == context.DeadlineExceeded } +func (e *opTimeoutError) Unwrap() error { return e.cause } +func (e *opTimeoutError) Timeout() bool { return true } + +// directDeadline is the deadline a direct conn's socket gets for the running +// command: the earlier of the caller's and the per-operation one. +func (c *memcachedConn) directDeadline(ctx context.Context) (time.Time, bool) { + dl, ok := ctx.Deadline() + if !c.opDeadline.IsZero() && (!ok || c.opDeadline.Before(dl)) { + return c.opDeadline, true + } + return dl, ok +} + +// armOpTimer arms the conn's timer for what is left of the per-operation limit and +// returns its channel; nil (a channel that never fires) if the command has no +// limit. Called only when a loop conn is about to park: a reply that comes within +// the spin window costs nothing. +func (c *memcachedConn) armOpTimer() <-chan time.Time { + if c.opDeadline.IsZero() { + return nil + } + d := time.Until(c.opDeadline) + if c.opTimer == nil { + c.opTimer = time.NewTimer(d) + } else { + c.opTimer.Reset(d) + } + return c.opTimer.C +} + +func (c *memcachedConn) disarmOpTimer() { + if c.opTimer != nil && !c.opDeadline.IsZero() { + c.opTimer.Stop() + } +} + +// opTimedOut ends the running command because its per-operation limit ran out: the +// conn is closed (the server may still send a reply) and the error says so. +func (c *memcachedConn) opTimedOut(cause error) error { + err := &opTimeoutError{d: c.opTimeout, cause: cause} + _ = c.closeWithErr(err) + return err +} + +// opError turns the error of a command into what the caller should see. A +// deadline that fired on a direct conn's socket is reported as what ended it: the +// caller's context if that has ended (its error, wrapped around the socket's), the +// per-operation timeout if that was the limit, the caller's deadline if that has +// been reached. An application answer is never relabelled, whatever the time. +// Call it before endOp (commands.go): it reads the limit beginOp put on the conn. +func (c *memcachedConn) opError(ctx context.Context, err error) error { + if err == nil || !errors.Is(err, os.ErrDeadlineExceeded) { + return err + } + if cerr := ctx.Err(); cerr != nil { + return fmt.Errorf("%w: %w", cerr, err) } + if c.opTimeout != 0 { + return &opTimeoutError{d: c.opTimeout, cause: err} + } + if dl, ok := ctx.Deadline(); ok && !time.Now().Before(dl) { + return fmt.Errorf("%w: %w", context.DeadlineExceeded, err) + } + return err } // closeWithErr records err as the close cause and closes the conn. Safe to @@ -643,6 +738,8 @@ func (c *memcachedConn) wait(ctx context.Context, req *mcRequest) error { default: } } + tc := c.armOpTimer() + defer c.disarmOpTimer() select { case <-req.doneCh: return req.resultErr @@ -653,6 +750,8 @@ func (c *memcachedConn) wait(ctx context.Context, req *mcRequest) error { // is poisoned — close it so the pool discards it. _ = c.closeWithErr(ctx.Err()) return ctx.Err() + case <-tc: + return c.opTimedOut(nil) } } @@ -737,7 +836,7 @@ func (c *memcachedConn) execTextDirect(ctx context.Context, req *mcRequest, enco c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { @@ -785,7 +884,7 @@ func (c *memcachedConn) execBinaryDirect(ctx context.Context, req *mcRequest, en c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { @@ -834,7 +933,7 @@ func (c *memcachedConn) execBinaryMultiDirect(ctx context.Context, req *mcReques c.syncBuf = make([]byte, 16<<10) } - if dl, ok := ctx.Deadline(); ok { + if dl, ok := c.directDeadline(ctx); ok { _ = c.tcp.SetDeadline(dl) defer func() { if c.tcp != nil { diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go new file mode 100644 index 000000000..dd2cbcc15 --- /dev/null +++ b/driver/memcached/op_alloc_1218_test.go @@ -0,0 +1,40 @@ +//go:build !race + +package memcached + +import ( + "context" + "testing" + "time" +) + +// The per-operation limit costs a command no allocation: a client with Timeout +// (the 3 s default or a set one) allocates exactly what one with the limit off does, +// on the direct and the loop paths. (A context built per command cost 4 allocations +// and broke the strict budgets of TestAllocBudgets; the CI step that runs them +// skips -race, as this file does.) +func TestOperationLimitDoesNotAllocate1218(t *testing.T) { + f := startFake(t) + f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + ctx := context.Background() + allocs := func(timeout time.Duration) float64 { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: timeout}) + if _, err := c.Get(ctx, "k"); err != nil { // warm the pool + t.Fatal(err) + } + return testing.AllocsPerRun(200, func() { + if _, err := c.Get(ctx, "k"); err != nil { + t.Fatal(err) + } + }) + } + on, off := allocs(3*time.Second), allocs(-1) + t.Logf("Get: %.1f allocs/op with the limit, %.1f without", on, off) + if on > off { + t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", on-off) + } + }) + } +} diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go index 94b4c431b..e05dbfbca 100644 --- a/driver/memcached/op_timeout_1218_test.go +++ b/driver/memcached/op_timeout_1218_test.go @@ -11,6 +11,7 @@ import ( "errors" "io" "net" + "os" "strings" "sync" "testing" @@ -200,8 +201,8 @@ func TestOperationKeepsTheCallersShorterDeadline1218(t *testing.T) { _, err := c.Get(ctx, "k") return err }) - if !errors.Is(err, context.DeadlineExceeded) && !isTimeout(err) { - t.Fatalf("error %v, want a deadline error", err) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("error %v, want one matching context.DeadlineExceeded (also on the direct path)", err) } if strings.Contains(err.Error(), "operation timed out") { t.Errorf("error %q: the caller's own deadline is reported as Config.Timeout", err) @@ -213,11 +214,6 @@ func TestOperationKeepsTheCallersShorterDeadline1218(t *testing.T) { } } -func isTimeout(err error) bool { - var ne net.Error - return errors.As(err, &ne) && ne.Timeout() -} - // A client that answers within the bound is not affected, and the bound does not // stay on the conn: the conn is used again after the time it would have fired. // (Passes on main by design.) @@ -293,16 +289,15 @@ func TestNegativeTimeoutTurnsTheOperationBoundOff1218(t *testing.T) { } // A caller that cancels its context gets its own cancellation back, not relabelled -// as the driver's timeout, and the conn is dropped. (A direct conn does not watch a -// context's cancellation, only its deadline, so it is not in the cases.) +// as the driver's timeout, and the conn is dropped. This includes the direct path, +// the one a client without an engine uses: its blocked read is woken by the cancel, +// it does not wait for Config.Timeout, and the error matches context.Canceled, not a +// bare i/o timeout. func TestOperationKeepsTheCallersCancellation1218(t *testing.T) { if defaultTimeout != 3*time.Second { t.Errorf("defaultTimeout = %v, want the documented 3 s", defaultTimeout) } for _, cl := range opClients { - if cl.name == "direct" { - continue - } t.Run(cl.name, func(t *testing.T) { t.Parallel() addr, _ := startMuteServer(t, false) @@ -325,3 +320,35 @@ func TestOperationKeepsTheCallersCancellation1218(t *testing.T) { }) } } + +// opError never relabels an application answer, whatever the time: only a deadline +// error (the socket's, on a direct conn) is turned into the per-operation timeout, +// and a caller's ended context wins over it. +func TestOpErrorRelabelsOnlyDeadlineErrors1218(t *testing.T) { + c := &memcachedConn{opTimeout: 300 * time.Millisecond} + if err := c.opError(context.Background(), ErrCacheMiss); err != ErrCacheMiss { + t.Errorf("an application answer was relabelled: %v", err) + } + sock := os.ErrDeadlineExceeded + err := c.opError(context.Background(), sock) + if !errors.Is(err, context.DeadlineExceeded) || !errors.Is(err, os.ErrDeadlineExceeded) || !strings.Contains(err.Error(), "operation timed out after 300ms") { + t.Errorf("the socket's deadline error became %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + err = c.opError(ctx, sock) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("a caller's cancel became %v", err) + } + // No limit on the command: the socket's error is left alone while the caller's + // deadline has not been reached, and is the caller's once it has. + c.opTimeout = 0 + if err := c.opError(context.Background(), sock); err != sock { + t.Errorf("an error of an unlimited command was changed: %v", err) + } + dctx, dcancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Millisecond)) + defer dcancel() + if err := c.opError(dctx, sock); !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("a caller's deadline became %v", err) + } +} diff --git a/driver/memcached/options.go b/driver/memcached/options.go index b7cfcd29e..8687056bf 100644 --- a/driver/memcached/options.go +++ b/driver/memcached/options.go @@ -68,7 +68,8 @@ type Config struct { // matches [context.DeadlineExceeded], and the conn it used is dropped. It // applies whether or not the caller's context has a deadline; a deadline of the // caller's that is not later than Timeout wins and its error is returned as it - // is. It does not cover waiting for a pool slot or dialling a new conn (that is + // is. A multi-key get, Stats and Flush are one operation each: Timeout covers + // the whole command, not each key or each read. It does not cover waiting for a pool slot or dialling a new conn (that is // [Config.DialTimeout]'s). Zero means the default, 3s; a negative value turns // the per-operation deadline off, and the caller's context is then the only // bound. From 0b4853f9f52978b788913f1c006eb07043aad1d3 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 20:24:43 +0100 Subject: [PATCH 4/8] driver/memcached: the allocation comparison test is not a !race file (the CI guard step names every !race test of a package) (Refs #1218) --- driver/memcached/op_alloc_1218_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go index dd2cbcc15..3b9f25ed9 100644 --- a/driver/memcached/op_alloc_1218_test.go +++ b/driver/memcached/op_alloc_1218_test.go @@ -1,5 +1,3 @@ -//go:build !race - package memcached import ( @@ -12,7 +10,7 @@ import ( // (the 3 s default or a set one) allocates exactly what one with the limit off does, // on the direct and the loop paths. (A context built per command cost 4 allocations // and broke the strict budgets of TestAllocBudgets; the CI step that runs them -// skips -race, as this file does.) +// skips -race; the comparison below holds under -race too, both sides being inflated alike.) func TestOperationLimitDoesNotAllocate1218(t *testing.T) { f := startFake(t) f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} From 231f3c18eb0afe1cc20a8600417466696a1f95dc Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 22:07:44 +0100 Subject: [PATCH 5/8] driver/memcached: the wake of a direct read lives on the conn (a cancellable context costs 2 allocations, not 5); tests for the health-check ping after a limited command, the limit on a reused conn, and the cancellable-context budget (Refs #1218) --- driver/memcached/commands.go | 25 +++--- driver/memcached/conn.go | 77 ++++++++++------- driver/memcached/op_alloc_1218_test.go | 31 +++++-- driver/memcached/op_timeout_1218_test.go | 103 +++++++++++++++++++++++ 4 files changed, 187 insertions(+), 49 deletions(-) diff --git a/driver/memcached/commands.go b/driver/memcached/commands.go index 81f89d3f1..80e12d8a8 100644 --- a/driver/memcached/commands.go +++ b/driver/memcached/commands.go @@ -139,22 +139,19 @@ func (c *Client) opLimit(ctx context.Context) (timeout time.Duration, deadline t // beginOp puts the per-operation limit on conn for one command and, for a direct // conn (which waits in a socket read that does not look at a context), arms the wake -// of its read on the end of a cancellable ctx. It returns the func that disarms the -// wake, nil when none was armed (a ctx that cannot be cancelled costs nothing, not -// even a closure). Pair it with endOp(disarm), after opError. -func (c *Client) beginOp(ctx context.Context, conn *memcachedConn) (disarm func()) { +// of its read on the end of a cancellable ctx (armWake). A ctx that cannot be +// cancelled costs nothing; a cancellable one costs context.AfterFunc's two +// allocations. Pair it with endOp, after opError. +func (c *Client) beginOp(ctx context.Context, conn *memcachedConn) { conn.opTimeout, conn.opDeadline = c.opLimit(ctx) if conn.useDirect && ctx.Done() != nil { - return conn.wakeReadOnDone(ctx) + conn.armWake(ctx) } - return nil } // endOp ends the limit of the command that ran on conn and disarms the wake. -func endOp(conn *memcachedConn, disarm func()) { - if disarm != nil { - disarm() - } +func endOp(conn *memcachedConn) { + conn.disarmWake() conn.opTimeout, conn.opDeadline = 0, time.Time{} } @@ -167,9 +164,9 @@ func (c *Client) run(ctx context.Context, fn func(ctx context.Context, conn *mem if err != nil { return err } - disarm := c.beginOp(ctx, conn) + c.beginOp(ctx, conn) ferr := conn.opError(ctx, fn(ctx, conn)) - endOp(conn, disarm) + endOp(conn) if ferr != nil && (errors.Is(ferr, context.Canceled) || errors.Is(ferr, context.DeadlineExceeded)) { c.pool.discard(conn) return ferr @@ -905,9 +902,9 @@ func (c *Client) Ping(ctx context.Context) error { if err != nil { return err } - disarm := c.beginOp(ctx, conn) + c.beginOp(ctx, conn) perr := conn.opError(ctx, conn.Ping(ctx)) - endOp(conn, disarm) + endOp(conn) if perr != nil { if errors.Is(perr, context.Canceled) || errors.Is(perr, context.DeadlineExceeded) { c.pool.discard(conn) diff --git a/driver/memcached/conn.go b/driver/memcached/conn.go index 49d6373d6..0087c46c6 100644 --- a/driver/memcached/conn.go +++ b/driver/memcached/conn.go @@ -70,6 +70,15 @@ type memcachedConn struct { opDeadline time.Time opTimer *time.Timer + // The wake of a direct conn's blocked read on the end of the caller's context + // (armWake). The state is on the conn so that a command allocates none of it. + wakeMu sync.Mutex + wakeFired bool + wakeDone bool + wakeFn func() + wakeStop func() bool + wakeTCP *net.TCPConn + // Direct-mode fields. tcp *net.TCPConn // tcpFd caches the raw fd for MSG_DONTWAIT peeks. Pre-netpoll @@ -256,14 +265,11 @@ func (c *memcachedConn) handshake(ctx context.Context) error { // A direct conn waits in a socket read that does not look at a context (only // at the deadline execBinaryDirect set from it), so the end of the caller's // context, a cancellation too, wakes the read. - var stopWake func() if c.useDirect { - stopWake = c.wakeReadOnDone(hctx) + c.armWake(hctx) } err := c.runHandshake(hctx) - if stopWake != nil { - stopWake() - } + c.disarmWake() if err != nil && errors.Is(err, os.ErrDeadlineExceeded) && (ctx.Err() != nil || callerFirst) { // The caller's context ended the handshake (its deadline can fire on the // socket a hair before the context reports it): its error, not a bare @@ -286,32 +292,45 @@ func (c *memcachedConn) handshake(ctx context.Context) error { return err } -// wakeReadOnDone arranges that, when ctx ends, the socket of a direct conn gets a -// deadline of "now", which wakes a read blocked on a silent server (celeris#1213; -// a direct conn does not watch its context's cancellation, only its deadline). The -// returned func disarms it; it also undoes a wake that raced with a handshake that -// had just succeeded, so the conn is not left with a deadline in the past. -func (c *memcachedConn) wakeReadOnDone(ctx context.Context) (disarm func()) { - var mu sync.Mutex - fired, done := false, false - tcp := c.tcp // Close sets c.tcp to nil - stop := context.AfterFunc(ctx, func() { - mu.Lock() - defer mu.Unlock() - if !done { - fired = true - _ = tcp.SetDeadline(time.Now()) - } - }) - return func() { - stop() - mu.Lock() - defer mu.Unlock() - done = true - if fired && !c.closed.Load() { - _ = tcp.SetDeadline(time.Time{}) +// armWake arranges that, when ctx ends, the socket of a direct conn gets a deadline +// of "now", which wakes a read blocked on a silent server (celeris#1213; a direct +// conn does not watch its context's cancellation, only its deadline). Its state +// lives on the conn and the callback is built once per conn, so what a command pays +// for it is context.AfterFunc's own two allocations and nothing else (celeris#1218). +// disarmWake ends it; it also undoes a wake that raced with a command that had +// just succeeded, so the conn is not left with a deadline in the past. +func (c *memcachedConn) armWake(ctx context.Context) { + if c.wakeFn == nil { + tcp := c.tcp // Close sets c.tcp to nil + c.wakeTCP = tcp + c.wakeFn = func() { + c.wakeMu.Lock() + defer c.wakeMu.Unlock() + if !c.wakeDone { + c.wakeFired = true + _ = tcp.SetDeadline(time.Now()) + } } } + c.wakeMu.Lock() + c.wakeFired, c.wakeDone = false, false + c.wakeMu.Unlock() + c.wakeStop = context.AfterFunc(ctx, c.wakeFn) +} + +// disarmWake ends the wake armed by armWake; it does nothing if none is armed. +func (c *memcachedConn) disarmWake() { + if c.wakeStop == nil { + return + } + c.wakeStop() + c.wakeStop = nil + c.wakeMu.Lock() + defer c.wakeMu.Unlock() + c.wakeDone = true + if c.wakeFired && !c.closed.Load() { + _ = c.wakeTCP.SetDeadline(time.Time{}) + } } // runHandshake is the binary handshake without the bound: a VERSION round trip. diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go index 3b9f25ed9..d68c775e6 100644 --- a/driver/memcached/op_alloc_1218_test.go +++ b/driver/memcached/op_alloc_1218_test.go @@ -8,16 +8,22 @@ import ( // The per-operation limit costs a command no allocation: a client with Timeout // (the 3 s default or a set one) allocates exactly what one with the limit off does, -// on the direct and the loop paths. (A context built per command cost 4 allocations -// and broke the strict budgets of TestAllocBudgets; the CI step that runs them -// skips -race; the comparison below holds under -race too, both sides being inflated alike.) +// on the direct and the loop paths, with a context that cannot be cancelled. (A +// context built per command cost 4 allocations and broke the strict budgets of +// TestAllocBudgets; the CI step that runs them skips -race; the comparison below +// holds under -race too, both sides being inflated alike.) +// +// A context that CAN be cancelled (WithCancel, WithTimeout, an HTTP/2 stream's) +// costs a direct conn context.AfterFunc's two allocations, which arm the wake of +// its blocked read (the loop paths select on the context and cost nothing). That +// price is budgeted here, explicitly: direct at most 2 more than the plain command, +// loop paths at most 0 more. (The first version of the wake cost 5.) func TestOperationLimitDoesNotAllocate1218(t *testing.T) { f := startFake(t) f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} for _, cl := range opClients { t.Run(cl.name, func(t *testing.T) { - ctx := context.Background() - allocs := func(timeout time.Duration) float64 { + allocs := func(timeout time.Duration, ctx context.Context) float64 { c := cl.new(t, Config{Addr: f.Addr(), Timeout: timeout}) if _, err := c.Get(ctx, "k"); err != nil { // warm the pool t.Fatal(err) @@ -28,11 +34,24 @@ func TestOperationLimitDoesNotAllocate1218(t *testing.T) { } }) } - on, off := allocs(3*time.Second), allocs(-1) + bg := context.Background() + on, off := allocs(3*time.Second, bg), allocs(-1, bg) t.Logf("Get: %.1f allocs/op with the limit, %.1f without", on, off) if on > off { t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", on-off) } + + cctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cancellable := allocs(3*time.Second, cctx) + extra := 0.0 + if cl.name == "direct" { + extra = 2 + } + t.Logf("Get with a cancellable context: %.1f allocs/op (plain %.1f, budget plain+%.0f)", cancellable, off, extra) + if cancellable > off+extra { + t.Errorf("a cancellable context costs %.1f allocations per command, budget %.0f", cancellable-off, extra) + } }) } } diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go index e05dbfbca..c27e1901a 100644 --- a/driver/memcached/op_timeout_1218_test.go +++ b/driver/memcached/op_timeout_1218_test.go @@ -5,6 +5,7 @@ package memcached // the caller until its context ended: for ever under context.Background(). import ( + "bufio" "bytes" "context" "encoding/binary" @@ -352,3 +353,105 @@ func TestOpErrorRelabelsOnlyDeadlineErrors1218(t *testing.T) { t.Errorf("a caller's deadline became %v", err) } } + +// The pool's health check pings an idle conn with conn.Ping, outside any Client +// command, after the command that last used it. The limit of that command must have +// been cleared: a stale deadline in the past would fail every ping at once, and the +// check would close every idle conn. +func TestHealthCheckPingAfterALimitedCommand1218(t *testing.T) { + f := startFake(t) + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + c := cl.new(t, Config{Addr: f.Addr(), Timeout: 40 * time.Millisecond}) + ctx := context.Background() + if err := c.Set(ctx, "k"+cl.name, "v", 0); err != nil { + t.Fatal(err) + } + time.Sleep(80 * time.Millisecond) // past that command's limit + conn, err := c.pool.acquire(ctx, -1) + if err != nil { + t.Fatal(err) + } + defer c.pool.release(conn) + for i := 0; i < 5; i++ { + if err := conn.Ping(ctx); err != nil { + t.Fatalf("health-style Ping %d after a limited command: %v", i, err) + } + } + }) + } +} + +// startSlowFirstServer answers the first command of each conn after a delay (the +// caller parks for it) and none after it (it stalls): the second command on a +// reused conn is the one the limit must end. +func startSlowFirstServer(t *testing.T, delay time.Duration) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var mu sync.Mutex + var conns []net.Conn + t.Cleanup(func() { + _ = ln.Close() + mu.Lock() + defer mu.Unlock() + for _, c := range conns { + _ = c.Close() + } + }) + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + mu.Lock() + conns = append(conns, c) + mu.Unlock() + go func() { + r := bufio.NewReader(c) + if _, err := r.ReadString('\n'); err != nil { + return + } + time.Sleep(delay) + if _, err := c.Write([]byte("VERSION 1.6.0\r\n")); err != nil { + return + } + _, _ = io.Copy(io.Discard, r) // the next command is never answered + }() + } + }() + return ln.Addr().String() +} + +// The limit is armed again on a conn that is used again: the first command parks and +// is answered, the second on the same conn stalls and must end at about Timeout, not +// run unbounded. (Every other bounded test uses a fresh conn, whose first park builds +// the timer; a loop conn that does not re-arm its timer on reuse leaves every command +// after the first unbounded.) +func TestLimitIsArmedAgainOnAReusedConn1218(t *testing.T) { + const bound = 150 * time.Millisecond + for _, cl := range opClients { + t.Run(cl.name, func(t *testing.T) { + t.Parallel() + addr := startSlowFirstServer(t, 40*time.Millisecond) + c := cl.new(t, Config{Addr: addr, Timeout: bound}) + ctx := context.Background() + if _, err := c.Version(ctx); err != nil { + t.Fatalf("the first command: %v", err) + } + elapsed, err := opWithin(t, 2*time.Second, func() error { + _, err := c.Version(ctx) + return err + }) + if !errors.Is(err, context.DeadlineExceeded) || !strings.Contains(err.Error(), "timed out") { + t.Errorf("second command error %v, want the operation timeout", err) + } + if elapsed < bound/2 { + t.Errorf("second command returned after %v, before the bound of %v", elapsed, bound) + } + }) + } +} From 8339b6121aa6563881f89b9f55b74b9d5f5d44b9 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 22:21:51 +0100 Subject: [PATCH 6/8] driver/memcached: budget a cancellable context in TestAllocBudgets (CI runs it without -race); the -race run of the comparison test keeps only the limit-vs-off case (Refs #1218) --- driver/memcached/alloc_guard_test.go | 27 ++++++++++++++++++++++++++ driver/memcached/op_alloc_1218_test.go | 15 ++------------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/driver/memcached/alloc_guard_test.go b/driver/memcached/alloc_guard_test.go index a5f307150..15f9d198e 100644 --- a/driver/memcached/alloc_guard_test.go +++ b/driver/memcached/alloc_guard_test.go @@ -65,6 +65,11 @@ func TestAllocBudgets(t *testing.T) { // per-request slab allocation that replaces the per-value copies // — tracked for v1.4.1. {name: "GetMulti_10", budget: 26, bench: benchGetMulti10}, + // GET with a context that can be cancelled (WithCancel, WithTimeout, an + // HTTP/2 stream's): on the direct path the blocked read is woken when the + // context ends, which costs context.AfterFunc's two allocations on top of + // the 3 of GET_small (celeris#1218). The wake's own state lives on the conn. + {name: "GET_small_cancellable", budget: 5, bench: benchGETSmallCancellable}, {name: "PoolAcquireRelease", budget: 0, bench: benchPoolAcquireRelease}, } @@ -102,6 +107,28 @@ func benchGETSmall(b *testing.B) { } } +func benchGETSmallCancellable(b *testing.B) { + fake := startFakeB(b) + fake.store.kv["k"] = memItem{value: []byte("v"), cas: 1} + c, err := NewClient(fake.Addr()) + if err != nil { + b.Fatal(err) + } + defer func() { _ = c.Close() }() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if _, err := c.Get(ctx, "k"); err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + if _, err := c.Get(ctx, "k"); err != nil { + b.Fatal(err) + } + } +} + func benchSETSmall(b *testing.B) { fake := startFakeB(b) c, err := NewClient(fake.Addr()) diff --git a/driver/memcached/op_alloc_1218_test.go b/driver/memcached/op_alloc_1218_test.go index d68c775e6..089b97c8e 100644 --- a/driver/memcached/op_alloc_1218_test.go +++ b/driver/memcached/op_alloc_1218_test.go @@ -16,8 +16,8 @@ import ( // A context that CAN be cancelled (WithCancel, WithTimeout, an HTTP/2 stream's) // costs a direct conn context.AfterFunc's two allocations, which arm the wake of // its blocked read (the loop paths select on the context and cost nothing). That -// price is budgeted here, explicitly: direct at most 2 more than the plain command, -// loop paths at most 0 more. (The first version of the wake cost 5.) +// price is budgeted in TestAllocBudgets (GET_small_cancellable, which CI runs +// without -race): AllocsPerRun is too noisy under -race to hold it here. func TestOperationLimitDoesNotAllocate1218(t *testing.T) { f := startFake(t) f.store.kv["k"] = memItem{value: []byte("v"), cas: 1} @@ -41,17 +41,6 @@ func TestOperationLimitDoesNotAllocate1218(t *testing.T) { t.Errorf("the per-operation limit costs %.1f allocations per command, want 0", on-off) } - cctx, cancel := context.WithCancel(context.Background()) - defer cancel() - cancellable := allocs(3*time.Second, cctx) - extra := 0.0 - if cl.name == "direct" { - extra = 2 - } - t.Logf("Get with a cancellable context: %.1f allocs/op (plain %.1f, budget plain+%.0f)", cancellable, off, extra) - if cancellable > off+extra { - t.Errorf("a cancellable context costs %.1f allocations per command, budget %.0f", cancellable-off, extra) - } }) } } From 492ad7c115b30c658463ecde8d5b279a61c5f9b2 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 23:39:41 +0100 Subject: [PATCH 7/8] driver/memcached: tests for a late wake landing in the next command and for a cancel on a reused direct conn; a nil-in-production hook delays the wake callback (the late-wake test fails here) (Refs #1218) --- driver/memcached/conn.go | 8 +++ driver/memcached/op_timeout_1218_test.go | 86 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/driver/memcached/conn.go b/driver/memcached/conn.go index 0087c46c6..2cc4f340f 100644 --- a/driver/memcached/conn.go +++ b/driver/memcached/conn.go @@ -229,6 +229,11 @@ func (c *memcachedConn) pollIsDone() bool { // A test sets it to avoid waiting 5 s, and resets it to nil when it is done. var testHookDefaultHandshakeTimeout atomic.Pointer[time.Duration] +// testHookWakeDelay, when set, makes the wake callback of armWake sleep that long +// before it acts, so that a test can hold a callback in flight across the end of +// its command (celeris#1218). It is nil in production. +var testHookWakeDelay atomic.Pointer[time.Duration] + // handshake runs per-protocol validation. Text protocol has no handshake; // for binary we issue a VERSION opcode to confirm the server speaks binary. // (Some older stacks accept the TCP connection but send back a text ERROR @@ -304,6 +309,9 @@ func (c *memcachedConn) armWake(ctx context.Context) { tcp := c.tcp // Close sets c.tcp to nil c.wakeTCP = tcp c.wakeFn = func() { + if d := testHookWakeDelay.Load(); d != nil { + time.Sleep(*d) + } c.wakeMu.Lock() defer c.wakeMu.Unlock() if !c.wakeDone { diff --git a/driver/memcached/op_timeout_1218_test.go b/driver/memcached/op_timeout_1218_test.go index c27e1901a..d7ac24950 100644 --- a/driver/memcached/op_timeout_1218_test.go +++ b/driver/memcached/op_timeout_1218_test.go @@ -455,3 +455,89 @@ func TestLimitIsArmedAgainOnAReusedConn1218(t *testing.T) { }) } } + +// The wake callback of a command that ended as its context was cancelled can still +// be in flight when the next command starts on the same direct conn; it must not +// give that command, whose context is alive, a deadline of "now" (a false "operation +// timed out" and a lost healthy conn). The hook holds the callback of the first +// command for 100 ms so that it lands after the second one has armed its own wake. +func TestLateWakeDoesNotFailTheNextCommand1218(t *testing.T) { + d := 100 * time.Millisecond + testHookWakeDelay.Store(&d) + t.Cleanup(func() { testHookWakeDelay.Store(nil) }) + for _, proto := range []struct { + name string + p Protocol + start func(t *testing.T) string + }{ + {"text", ProtocolText, func(t *testing.T) string { return startFake(t).Addr() }}, + {"binary", ProtocolBinary, func(t *testing.T) string { return startFakeBinary(t).Addr() }}, + } { + t.Run(proto.name, func(t *testing.T) { + c := opClients[0].new(t, Config{Addr: proto.start(t), Protocol: proto.p, Timeout: 30 * time.Second}) + conn, err := c.pool.acquire(context.Background(), -1) + if err != nil { + t.Fatal(err) + } + defer c.pool.release(conn) + // First command: its context ends as it finishes, the stop of its wake + // fails (the callback has started) and the callback is held by the hook. + ctx1, cancel1 := context.WithCancel(context.Background()) + conn.armWake(ctx1) + cancel1() + conn.disarmWake() + // Second command, on a live context. + ctx2, cancel2 := context.WithCancel(context.Background()) + defer cancel2() + conn.armWake(ctx2) + defer conn.disarmWake() + time.Sleep(3 * d) // the held callback of the first command runs now, if it still can + if err := conn.Ping(ctx2); err != nil { + t.Fatalf("a live command on the conn after a cancelled one: %v", err) + } + }) + } +} + +// A cancellation reaches a command on a conn that has already served one: the wake +// is armed again for every command, so the cancel ends it at once instead of at +// Config.Timeout. (Every other cancellation test uses a fresh conn; one whose wake +// state is not reset per command has it spent by the first command's disarm, and +// every cancel after it waits for the limit. For the binary protocol the handshake +// is that first use.) The first command runs under a cancellable context, since a +// context that cannot be cancelled arms no wake. +func TestCancelOnAReusedDirectConn1218(t *testing.T) { + for _, proto := range []struct { + name string + p Protocol + start func(t *testing.T) string + first func(ctx context.Context, c *Client) error + }{ + {"text", ProtocolText, func(t *testing.T) string { return startSlowFirstServer(t, 10*time.Millisecond) }, + func(ctx context.Context, c *Client) error { _, err := c.Version(ctx); return err }}, + {"binary", ProtocolBinary, func(t *testing.T) string { a, _ := startMuteServer(t, true); return a }, + func(ctx context.Context, c *Client) error { return nil }}, // the handshake was the first use + } { + t.Run(proto.name, func(t *testing.T) { + t.Parallel() + c := opClients[0].new(t, Config{Addr: proto.start(t), Protocol: proto.p, Timeout: 30 * time.Second}) + ctx1, cancel1 := context.WithCancel(context.Background()) + defer cancel1() + if err := proto.first(ctx1, c); err != nil { + t.Fatalf("the first command: %v", err) + } + ctx2, cancel2 := context.WithCancel(context.Background()) + time.AfterFunc(150*time.Millisecond, cancel2) + elapsed, err := opWithin(t, 3*time.Second, func() error { + _, err := c.Get(ctx2, "k") + return err + }) + if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation timed out") { + t.Errorf("error %v, want the caller's context.Canceled", err) + } + if elapsed > time.Second { + t.Errorf("returned after %v, want about the cancel at 150 ms and not the 30 s Timeout", elapsed) + } + }) + } +} From ee7e5a17b09ec9ca8669059504376f5f55477847 Mon Sep 17 00:00:00 2001 From: Albert Bausili Date: Mon, 2 Nov 2026 23:39:41 +0100 Subject: [PATCH 8/8] driver/memcached: disarmWake waits for a wake callback that has started, so it cannot run during the next command on the conn and give it a false timeout (Refs #1218) --- driver/memcached/conn.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/driver/memcached/conn.go b/driver/memcached/conn.go index 2cc4f340f..ebe24ca5e 100644 --- a/driver/memcached/conn.go +++ b/driver/memcached/conn.go @@ -77,7 +77,11 @@ type memcachedConn struct { wakeDone bool wakeFn func() wakeStop func() bool - wakeTCP *net.TCPConn + // wakeRan has capacity 1: the callback signals it when it has run, and + // disarmWake waits on it whenever stopping the wake failed, so that a callback + // of one command never lands in the next one. + wakeRan chan struct{} + wakeTCP *net.TCPConn // Direct-mode fields. tcp *net.TCPConn @@ -303,21 +307,28 @@ func (c *memcachedConn) handshake(ctx context.Context) error { // lives on the conn and the callback is built once per conn, so what a command pays // for it is context.AfterFunc's own two allocations and nothing else (celeris#1218). // disarmWake ends it; it also undoes a wake that raced with a command that had -// just succeeded, so the conn is not left with a deadline in the past. +// just succeeded, so the conn is not left with a deadline in the past. A callback +// that has started when the command ends is waited for there (wakeRan), so it +// cannot run during the next command on the same conn and fail it. func (c *memcachedConn) armWake(ctx context.Context) { if c.wakeFn == nil { tcp := c.tcp // Close sets c.tcp to nil c.wakeTCP = tcp + c.wakeRan = make(chan struct{}, 1) c.wakeFn = func() { if d := testHookWakeDelay.Load(); d != nil { time.Sleep(*d) } c.wakeMu.Lock() - defer c.wakeMu.Unlock() if !c.wakeDone { c.wakeFired = true _ = tcp.SetDeadline(time.Now()) } + c.wakeMu.Unlock() + select { + case c.wakeRan <- struct{}{}: + default: + } } } c.wakeMu.Lock() @@ -331,7 +342,11 @@ func (c *memcachedConn) disarmWake() { if c.wakeStop == nil { return } - c.wakeStop() + if !c.wakeStop() { + // The context has ended and the callback has been started on its own + // goroutine (stop is called once per arm): it signals wakeRan when done. + <-c.wakeRan + } c.wakeStop = nil c.wakeMu.Lock() defer c.wakeMu.Unlock()