From 33c70b2f4ce800e72ea61f55ff3a0ccc10a6cb10 Mon Sep 17 00:00:00 2001 From: cxljs Date: Wed, 17 Sep 2025 22:32:24 +0800 Subject: [PATCH] fix: pipeline repeatedly sets the error (#3525) * fix: pipeline repeatedly sets the error Signed-off-by: Xiaolong Chen * add test Signed-off-by: Xiaolong Chen * CI Signed-off-by: Xiaolong Chen --------- Signed-off-by: Xiaolong Chen Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com> --- pipeline_test.go | 33 +++++++++++++++++++++++++++++++++ redis.go | 5 ++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pipeline_test.go b/pipeline_test.go index 15eacb3d..a95df3c9 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -60,6 +60,39 @@ var _ = Describe("pipelining", func() { Expect(cmds).To(BeEmpty()) }) + It("pipeline: basic exec", func() { + p := client.Pipeline() + p.Get(ctx, "key") + p.Set(ctx, "key", "value", 0) + p.Get(ctx, "key") + cmds, err := p.Exec(ctx) + Expect(err).To(Equal(redis.Nil)) + Expect(cmds).To(HaveLen(3)) + Expect(cmds[0].Err()).To(Equal(redis.Nil)) + Expect(cmds[1].(*redis.StatusCmd).Val()).To(Equal("OK")) + Expect(cmds[1].Err()).NotTo(HaveOccurred()) + Expect(cmds[2].(*redis.StringCmd).Val()).To(Equal("value")) + Expect(cmds[2].Err()).NotTo(HaveOccurred()) + }) + + It("pipeline: exec pipeline when get conn failed", func() { + p := client.Pipeline() + p.Get(ctx, "key") + p.Set(ctx, "key", "value", 0) + p.Get(ctx, "key") + + client.Close() + + cmds, err := p.Exec(ctx) + Expect(err).To(Equal(redis.ErrClosed)) + Expect(cmds).To(HaveLen(3)) + for _, cmd := range cmds { + Expect(cmd.Err()).To(Equal(redis.ErrClosed)) + } + + client = redis.NewClient(redisOptions()) + }) + assertPipeline := func() { It("returns no errors when there are no commands", func() { _, err := pipe.Exec(ctx) diff --git a/redis.go b/redis.go index 16e21309..6e291a53 100644 --- a/redis.go +++ b/redis.go @@ -630,7 +630,10 @@ func (c *baseClient) generalProcessPipeline( return err }) if lastErr == nil || !canRetry || !shouldRetry(lastErr, true) { - setCmdsErr(cmds, lastErr) + // The error should be set here only when failing to obtain the conn. + if !isRedisError(lastErr) { + setCmdsErr(cmds, lastErr) + } return lastErr } }