diff --git a/redis.go b/redis.go index f63933b0..a6a71067 100644 --- a/redis.go +++ b/redis.go @@ -415,11 +415,12 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error { } else { // DialTimeout is shorter, cap the wait at DialTimeout waitCtx, cancel = context.WithTimeout(ctx, dialTimeout) - defer cancel() } } else { // No command deadline, use DialTimeout to prevent waiting indefinitely waitCtx, cancel = context.WithTimeout(ctx, dialTimeout) + } + if cancel != nil { defer cancel() }