From b1fa1bee1477e9fa91891c469fd30e7eeff8eeaf Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 26 Nov 2026 04:06:34 +0000 Subject: [PATCH] feat(resp): flag pipeline failures that never reached the wire A pipeline that failed before it wrote its request cannot have changed the server. The error now says so, and respNotApplied reads that flag. It walks Unwrap by hand because yaegi has no errors.As. --- resp.go | 38 ++++++++++++++++-- resp_notapplied_test.go | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 resp_notapplied_test.go diff --git a/resp.go b/resp.go index c1d168f..cf79365 100644 --- a/resp.go +++ b/resp.go @@ -318,7 +318,7 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any pc, err := c.acquire(deadline) if err != nil { - return nil, err + return nil, ¬AppliedError{err: err} } defer func() { c.free <- pc }() @@ -328,20 +328,50 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any } closeConn(pc) if isTimeout(err) { - return nil, err + return nil, markNotApplied(err, wrote) } if wrote && readAny && mutates { return nil, err } - replies, _, _, err = c.attemptEncodedOn(pc, payload, n, deadline) + replies, wrote, _, err = c.attemptEncodedOn(pc, payload, n, deadline) if err != nil { closeConn(pc) - return nil, err + return nil, markNotApplied(err, wrote) } return replies, nil } +// notAppliedError marks a pipeline failure that happened before the request reached the wire, +// so the server cannot have applied any command in it. +type notAppliedError struct{ err error } + +func (e *notAppliedError) Error() string { return e.err.Error() } +func (e *notAppliedError) Unwrap() error { return e.err } + +func markNotApplied(err error, wrote bool) error { + if wrote { + return err + } + return ¬AppliedError{err: err} +} + +// respNotApplied reports whether err says the failed call never wrote its request. +// It walks Unwrap by hand because errors.As is unsupported under yaegi. +func respNotApplied(err error) bool { + for err != nil { + if _, ok := err.(*notAppliedError); ok { + return true + } + u, ok := err.(interface{ Unwrap() error }) + if !ok { + return false + } + err = u.Unwrap() + } + return false +} + // pipelineMutatingCommands names every RESP command this client ever sends // that is NOT safe to blindly resend: each already changes server state on // success, so replaying it after an ambiguous failure (the request bytes diff --git a/resp_notapplied_test.go b/resp_notapplied_test.go new file mode 100644 index 0000000..8ce3b62 --- /dev/null +++ b/resp_notapplied_test.go @@ -0,0 +1,86 @@ +package traefikllmgateway + +import ( + "bufio" + "fmt" + "testing" +) + +func TestRESPPipelineNotApplied(t *testing.T) { + incr := [][]string{{"INCRBY", "k", "1"}, {"EXPIRE", "k", "60"}} + + t.Run("dial failure was never written", func(t *testing.T) { + ln := newFakeListener(t) + addr := ln.Addr().String() + _ = ln.Close() + c := newRESPClient(addr, "", 0) + _, err := c.pipeline(incr) + if err == nil { + t.Fatal("want an error dialing a dead address") + } + if !respNotApplied(err) { + t.Errorf("respNotApplied(%v) = false, want true", err) + } + if !respNotApplied(fmt.Errorf("wrapped: %w", err)) { + t.Error("respNotApplied lost the marker through fmt.Errorf %w") + } + }) + + t.Run("reply lost after bytes arrived is ambiguous", func(t *testing.T) { + ln := newFakeListener(t) + go func() { + conn, err := ln.Accept() + if err != nil { + return + } + _ = ln.Close() + defer func() { _ = conn.Close() }() + r := bufio.NewReader(conn) + _, _ = readRESPCommand(r) // SELECT + _, _ = conn.Write([]byte("+OK\r\n")) + _, _ = readRESPCommand(r) + _, _ = readRESPCommand(r) + _, _ = conn.Write([]byte(":1\r\n")) + }() + c := newRESPClient(ln.Addr().String(), "", 0) + _, err := c.pipeline(incr) + if err == nil { + t.Fatal("want an error") + } + if respNotApplied(err) { + t.Errorf("respNotApplied(%v) = true, want false: the INCRBY may have landed", err) + } + }) + + t.Run("timeout after the write is ambiguous", func(t *testing.T) { + ln := newFakeListener(t) + stop := make(chan struct{}) + t.Cleanup(func() { close(stop) }) + go func() { + conn, err := ln.Accept() + if err != nil { + return + } + defer func() { _ = conn.Close() }() + r := bufio.NewReader(conn) + _, _ = readRESPCommand(r) // SELECT + _, _ = conn.Write([]byte("+OK\r\n")) + _, _ = readRESPCommand(r) + <-stop + }() + c := newRESPClient(ln.Addr().String(), "", 0) + _, err := c.pipeline(incr) + if err == nil { + t.Fatal("want a timeout") + } + if respNotApplied(err) { + t.Errorf("respNotApplied(%v) = true, want false", err) + } + }) + + t.Run("nil and foreign errors are not marked", func(t *testing.T) { + if respNotApplied(nil) || respNotApplied(fmt.Errorf("boom")) { + t.Error("unmarked error reported as not applied") + } + }) +}