Skip to content

fix: Blocking commands respect canceled context #2433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,19 @@ func (c *ClusterClient) processPipelineNode(
defer func() {
node.Client.releaseConn(ctx, cn, processErr)
}()
processErr = c.processPipelineNodeConn(ctx, node, cn, cmds, failedCmds)

errCh := make(chan error, 1)

go func() {
errCh <- c.processPipelineNodeConn(ctx, node, cn, cmds, failedCmds)
}()

select {
case processErr = <-errCh:
case <-ctx.Done():
_ = cn.Close()
processErr = ctx.Err()
}

return processErr
})
Expand Down Expand Up @@ -1472,7 +1484,19 @@ func (c *ClusterClient) processTxPipelineNode(
defer func() {
node.Client.releaseConn(ctx, cn, processErr)
}()
processErr = c.processTxPipelineNodeConn(ctx, node, cn, cmds, failedCmds)

errCh := make(chan error, 1)

go func() {
errCh <- c.processTxPipelineNodeConn(ctx, node, cn, cmds, failedCmds)
}()

select {
case processErr = <-errCh:
case <-ctx.Done():
_ = cn.Close()
processErr = ctx.Err()
}

return processErr
})
Expand Down
38 changes: 38 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,24 @@ var _ = Describe("Commands", func() {
Expect(err).To(Equal(redis.Nil))
})

Describe("canceled context", func() {
It("should unblock XRead", func() {
ctx2, cancel := context.WithCancel(ctx)
errCh := make(chan error, 1)
go func() {
errCh <- client.XRead(ctx2, &redis.XReadArgs{
Streams: []string{"stream", "$"},
}).Err()
}()

var gotErr error
Consistently(errCh).ShouldNot(Receive(&gotErr), "Received %v", gotErr)
cancel()
Eventually(errCh).Should(Receive(&gotErr))
Expect(gotErr).To(HaveOccurred())
})
})

Describe("group", func() {
BeforeEach(func() {
err := client.XGroupCreate(ctx, "stream", "group", "0").Err()
Expand Down Expand Up @@ -5023,6 +5041,26 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})

Describe("canceled context", func() {
It("should unblock XReadGroup", func() {
ctx2, cancel := context.WithCancel(ctx)
errCh := make(chan error, 1)
go func() {
errCh <- client.XReadGroup(ctx2, &redis.XReadGroupArgs{
Group: "group",
Consumer: "consumer",
Streams: []string{"stream", ">"},
}).Err()
}()

var gotErr error
Consistently(errCh).ShouldNot(Receive(&gotErr), "Received %v", gotErr)
cancel()
Eventually(errCh).Should(Receive(&gotErr))
Expect(gotErr).To(HaveOccurred())
})
})
})

Describe("xinfo", func() {
Expand Down
17 changes: 17 additions & 0 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,21 @@ var _ = Describe("withConn", func() {
Expect(newConn).NotTo(Equal(conn))
Expect(client.connPool.Len()).To(Equal(1))
})

It("should remove the connection from the pool if the context is canceled", func() {
var conn *pool.Conn

ctx2, cancel := context.WithCancel(ctx)
cancel()

client.withConn(ctx2, func(ctx context.Context, c *pool.Conn) error {
conn = c
return nil
})

newConn, err := client.connPool.Get(ctx)
Expect(err).To(BeNil())
Expect(newConn).NotTo(Equal(conn))
Expect(client.connPool.Len()).To(Equal(1))
})
})
16 changes: 13 additions & 3 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,19 @@ func (c *PubSub) ReceiveTimeout(ctx context.Context, timeout time.Duration) (int
return nil, err
}

err = cn.WithReader(context.Background(), timeout, func(rd *proto.Reader) error {
return c.cmd.readReply(rd)
})
errCh := make(chan error, 1)

go func() {
errCh <- cn.WithReader(context.Background(), timeout, func(rd *proto.Reader) error {
return c.cmd.readReply(rd)
})
}()

select {
case err = <-errCh:
case <-ctx.Done():
err = ctx.Err()
}

c.releaseConnWithLock(ctx, cn, err, timeout > 0)

Expand Down
21 changes: 21 additions & 0 deletions pubsub_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis_test

import (
"context"
"io"
"net"
"sync"
Expand Down Expand Up @@ -567,4 +568,24 @@ var _ = Describe("PubSub", func() {
Expect(msg.Channel).To(Equal("mychannel"))
Expect(msg.Payload).To(Equal(text))
})

Describe("canceled context", func() {
It("should unblock ReceiveMessage", func() {
pubsub := client.Subscribe(ctx, "mychannel")
defer pubsub.Close()

ctx2, cancel := context.WithCancel(ctx)
errCh := make(chan error, 1)
go func() {
_, err := pubsub.ReceiveMessage(ctx2)
errCh <- err
}()

var gotErr error
Consistently(errCh).ShouldNot(Receive(&gotErr), "Received %v", gotErr)
cancel()
Eventually(errCh).Should(Receive(&gotErr))
Expect(gotErr).To(HaveOccurred())
})
})
})
13 changes: 12 additions & 1 deletion redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,18 @@ func (c *baseClient) withConn(
c.releaseConn(ctx, cn, fnErr)
}()

fnErr = fn(ctx, cn)
errCh := make(chan error, 1)

go func() {
errCh <- fn(ctx, cn)
}()

select {
case fnErr = <-errCh:
case <-ctx.Done():
_ = c.connPool.CloseConn(cn)
fnErr = ctx.Err()
}

return fnErr
}
Expand Down