Skip to content

Fix deadlock when an invalid URI is presented to DefaultClusterTopologyRefresh #3243

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

Merged
merged 2 commits into from
May 29, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,21 @@ private void openConnections(ConnectionTracker tracker, Iterable<RedisURI> redis

for (RedisURI redisURI : redisURIs) {

if (redisURI.getHost() == null || tracker.contains(redisURI) || !isEventLoopActive()) {
continue;
}
CompletableFuture<StatefulRedisConnection<String, String>> sync = new CompletableFuture<>();

try {

if (redisURI.getHost() == null || tracker.contains(redisURI) || !isEventLoopActive()) {
continue;
}

SocketAddress socketAddress = clientResources.socketAddressResolver().resolve(redisURI);

ConnectionFuture<StatefulRedisConnection<String, String>> connectionFuture = nodeConnectionFactory
.connectToNodeAsync(StringCodec.UTF8, socketAddress);

// Note: timeout skew due to potential socket address resolution and connection work possible.

CompletableFuture<StatefulRedisConnection<String, String>> sync = new CompletableFuture<>();
Timeout cancelTimeout = clientResources.timer().newTimeout(it -> {

String message = String.format("Unable to connect to [%s]: Timeout after %s", socketAddress,
Expand Down Expand Up @@ -360,7 +362,10 @@ private void openConnections(ConnectionTracker tracker, Iterable<RedisURI> redis

tracker.addConnection(redisURI, sync);
} catch (RuntimeException e) {
logger.warn(String.format("Unable to connect to [%s]", redisURI), e);
String message = String.format("Unable to connect to [%s]", redisURI);
logger.warn(message, e);
sync.completeExceptionally(new RedisConnectionException(message, e));
tracker.addConnection(redisURI, sync);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static io.lettuce.TestTags.UNIT_TEST;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Timeout.ThreadMode.SEPARATE_THREAD;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.anyLong;
Expand All @@ -40,6 +41,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -455,6 +457,20 @@ void shouldCloseConnections() {
verify(connection2).closeAsync();
}

/**
* @see <a href="https://github.com/redis/lettuce/issues/3240">Issue link</a>
*/
@Test
@org.junit.jupiter.api.Timeout(value = 5, unit = TimeUnit.SECONDS, threadMode = SEPARATE_THREAD)
void shouldHandleInvalidUrisWithoutDeadlock() {
List<RedisURI> seed = Arrays.asList(RedisURI.create("redis://localhost:$(INVALID_DATA):CONFIG"),
RedisURI.create("redis://localhost:$(INVALID_DATA):CONFIG"));
CompletionException completionException = Assertions.assertThrows(CompletionException.class,
() -> sut.loadViews(seed, Duration.ofSeconds(1), true).toCompletableFuture().join());
assertThat(completionException)
.hasRootCauseInstanceOf(DefaultClusterTopologyRefresh.CannotRetrieveClusterPartitions.class);
}

@Test
void undiscoveredAdditionalNodesShouldBeLastUsingClientCount() {

Expand Down