-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add DrainOnContextTimeout to return error on context expiration #3946
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1046,6 +1046,19 @@ func (c *baseClient) releaseConn(ctx context.Context, cn *pool.Conn, err error) | |
| // tracking is on. Limiter accounting stays with the callers, whose shapes | ||
| // differ. Shared by releaseConn and withPipelineConn so the two cannot drift. | ||
| func (c *baseClient) releaseConnToPool(ctx context.Context, p pool.Pooler, cn *pool.Conn, err error) { | ||
| // If the command finished with a context error and drain-on-timeout is | ||
| // enabled, try to consume the outstanding reply and restore protocol | ||
| // alignment before re-pooling the connection. The original context error is | ||
| // still preserved for the caller. | ||
| if c.shouldDrainOnContextTimeout(err) { | ||
| if c.drainConnOnContextTimeout(ctx, cn) { | ||
| p.Put(ctx, cn) | ||
| return | ||
| } | ||
| p.Remove(ctx, cn, err) | ||
| return | ||
| } | ||
|
|
||
| if isBadConn(err, false, c.opt.Addr) { | ||
| p.Remove(ctx, cn, err) | ||
| return | ||
|
|
@@ -1070,6 +1083,44 @@ func (c *baseClient) releaseConnToPool(ctx context.Context, p pool.Pooler, cn *p | |
| p.Put(ctx, cn) | ||
| } | ||
|
|
||
| func (c *baseClient) shouldDrainOnContextTimeout(err error) bool { | ||
| // Only activate this path for explicit opt-in behavior and for context | ||
| // deadline or cancellation errors, which are the cases where a bounded drain | ||
| // is still safe to attempt. | ||
| return c.opt != nil && c.opt.DrainOnContextTimeout && isContextError(err) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| func (c *baseClient) drainConnOnContextTimeout(ctx context.Context, cn *pool.Conn) bool { | ||
| if c.opt == nil || cn == nil || cn.IsClosed() { | ||
| return false | ||
| } | ||
|
|
||
| drainCtx, cancel := context.WithTimeout(context.Background(), c.opt.ContextTimeoutDrainTimeout) | ||
| defer cancel() | ||
|
|
||
| readErr := cn.WithReader(drainCtx, c.opt.ContextTimeoutDrainTimeout, func(rd *proto.Reader) error { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Negative drain timeout hangs releaseHigh Severity If Reviewed by Cursor Bugbot for commit 5939f7b. Configure here. |
||
| // RESP3 connections may already have pending push frames buffered ahead | ||
| // of the reply. Process them with the configured push processor so the | ||
| // stream remains aligned before we read the command reply. | ||
| if c.opt.Protocol == 3 && c.pushProcessor != nil { | ||
| if err := c.processPendingPushNotificationWithReader(drainCtx, cn, rd); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| _, err := rd.ReadReply() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Pipeline/TxPipeline calls, Useful? React with 👍 / 👎. |
||
| if err == nil || err == proto.Nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drain misreads client-handled pushesMedium Severity On RESP3, Reviewed by Cursor Bugbot for commit 5939f7b. Configure here. |
||
| return nil | ||
| } | ||
| return err | ||
| }) | ||
|
|
||
| if readErr != nil { | ||
| internal.Logger.Printf(ctx, "redis: context timeout drain failed for conn[%d]: %v", cn.GetID(), readErr) | ||
| return false | ||
| } | ||
| return true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pipeline drain reads one replyHigh Severity When Reviewed by Cursor Bugbot for commit 5939f7b. Configure here. |
||
| } | ||
|
|
||
| func (c *baseClient) withConn( | ||
| ctx context.Context, fn func(context.Context, *pool.Conn) error, | ||
| ) error { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drain path skips CSC probe
Medium Severity
The
releaseConnToPoolfunction's new context-timeout drain path returns early after a successful drain. This bypasses essential post-command processing, such as marking client-side cache read pending and updating HIMPORT hooks, potentially leading to client/server state divergence and incorrect cache invalidation.Reviewed by Cursor Bugbot for commit 5939f7b. Configure here.