From f618da855f1fe39d4ad55d3c5fd5e7ad0a7ed445 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Wed, 30 Sep 2026 17:03:01 +0000 Subject: [PATCH] Complete future with exception if message availability subscriptions fail --- .../push/RedisMessageAvailabilityManager.java | 8 +- .../RedisDynamoDbMessagePublisher.java | 17 +++- .../RedisMessageAvailabilityManagerTest.java | 86 +++++++++++++++++++ .../RedisDynamoDbMessagePublisherTest.java | 20 +++++ 4 files changed, 126 insertions(+), 5 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManager.java index dc87c0c2f..5e0170dba 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManager.java @@ -180,15 +180,15 @@ public class RedisMessageAvailabilityManager extends RedisClusterPubSubAdapter clusterClient.withBinaryCluster(connection -> connection.async() - .spublish(eventChannel, CLIENT_CONNECTED_EVENT_BYTES))) - .handle((ignored, throwable) -> { + .thenCompose(_ -> clusterClient.withBinaryCluster(connection -> connection.async() + .spublish(eventChannel, CLIENT_CONNECTED_EVENT_BYTES)) + .handle((_, throwable) -> { if (throwable != null) { PUBLISH_CLIENT_CONNECTION_EVENT_ERROR_COUNTER.increment(); } return null; - }); + })); } /** diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisher.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisher.java index c3836e431..c571c367d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisher.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisher.java @@ -9,6 +9,7 @@ import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name; import io.micrometer.core.instrument.Metrics; import java.util.UUID; +import java.util.concurrent.CompletionStage; import java.util.concurrent.Flow; import javax.annotation.Nullable; import org.reactivestreams.Publisher; @@ -155,11 +156,15 @@ class RedisDynamoDbMessagePublisher implements MessageAvailabilityListener, Flow this.subscriber = subscriber; + @Nullable final CompletionStage clientConnectedFuture; + if (!terminateOnQueueEmpty) { // If we're trying to follow a live stream of messages, then listen for signals indicating that new messages are // available in Redis, that messages have been persisted from Redis to DynamoDB, or that there's a conflicting // message reader connected somewhere else - redisMessageAvailabilityManager.handleClientConnected(accountIdentifier, device.getId(), this); + clientConnectedFuture = redisMessageAvailabilityManager.handleClientConnected(accountIdentifier, device.getId(), this); + } else { + clientConnectedFuture = null; } subscriber.onSubscribe(new Flow.Subscription() { @@ -173,6 +178,16 @@ class RedisDynamoDbMessagePublisher implements MessageAvailabilityListener, Flow terminate(); } }); + + // Defer the client connected handling until after onSubscribe() is called. Otherwise, onError() might be called + // first, which violates the Reactive Streams spec. + if (clientConnectedFuture != null) { + clientConnectedFuture.whenComplete((_, throwable) -> { + if (throwable != null) { + handleMessageSourceError(throwable); + } + }); + } } @Override diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManagerTest.java index 9912f5d9e..3955c80d0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/push/RedisMessageAvailabilityManagerTest.java @@ -5,7 +5,10 @@ package org.whispersystems.textsecuregcm.push; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -13,13 +16,16 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import io.lettuce.core.RedisException; import io.lettuce.core.cluster.SlotHash; +import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands; import io.lettuce.core.cluster.event.ClusterTopologyChangedEvent; import io.lettuce.core.cluster.models.partitions.RedisClusterNode; import io.lettuce.core.cluster.pubsub.api.async.RedisClusterPubSubAsyncCommands; import io.lettuce.core.cluster.pubsub.api.sync.RedisClusterPubSubCommands; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; @@ -184,6 +190,7 @@ class RedisMessageAvailabilityManagerTest { final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder() .binaryPubSubCommands(pubSubCommands) .binaryPubSubAsyncCommands(pubSubAsyncCommands) + .binaryAsyncCommands(buildBinaryAsyncCommands()) .build(); final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager( @@ -250,6 +257,7 @@ class RedisMessageAvailabilityManagerTest { final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder() .binaryPubSubAsyncCommands(pubSubAsyncCommands) + .binaryAsyncCommands(buildBinaryAsyncCommands()) .build(); final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager( @@ -281,4 +289,82 @@ class RedisMessageAvailabilityManagerTest { verify(pubSubAsyncCommands) .sunsubscribe(RedisMessageAvailabilityManager.getClientEventChannel(noListenerAccountIdentifier, noListenerDeviceId)); } + + @Test + void handleClientConnectedSubscribeFailure() { + @SuppressWarnings("unchecked") final RedisClusterPubSubAsyncCommands pubSubAsyncCommands = + mock(RedisClusterPubSubAsyncCommands.class); + + final RedisException exception = new RedisException("Currently not connected. Commands are rejected."); + when(pubSubAsyncCommands.ssubscribe(any())) + .thenReturn(MockRedisFuture.failedFuture(exception)); + + final RedisAdvancedClusterAsyncCommands binaryAsyncCommands = buildBinaryAsyncCommands(); + + final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder() + .binaryPubSubAsyncCommands(pubSubAsyncCommands) + .binaryAsyncCommands(binaryAsyncCommands) + .build(); + + final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager( + clusterClient, + Runnable::run, + Runnable::run); + + eventManager.start(); + + final CompletionException completionException = assertThrows(CompletionException.class, () -> + eventManager.handleClientConnected(UUID.randomUUID(), Device.PRIMARY_ID, new MessageAvailabilityAdapter()) + .toCompletableFuture() + .join()); + + assertEquals(exception, completionException.getCause()); + + // We shouldn't announce a client connection if we couldn't subscribe to events for that client + verify(binaryAsyncCommands, never()).spublish(any(), any()); + } + + @Test + void handleClientConnectedPublishFailure() { + @SuppressWarnings("unchecked") final RedisClusterPubSubAsyncCommands pubSubAsyncCommands = + mock(RedisClusterPubSubAsyncCommands.class); + + when(pubSubAsyncCommands.ssubscribe(any())).thenReturn(MockRedisFuture.completedFuture(null)); + + @SuppressWarnings("unchecked") final RedisAdvancedClusterAsyncCommands binaryAsyncCommands = + mock(RedisAdvancedClusterAsyncCommands.class); + + when(binaryAsyncCommands.spublish(any(), any())) + .thenReturn(MockRedisFuture.failedFuture(new RedisException("Currently not connected. Commands are rejected."))); + + final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder() + .binaryPubSubAsyncCommands(pubSubAsyncCommands) + .binaryAsyncCommands(binaryAsyncCommands) + .build(); + + final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager( + clusterClient, + Runnable::run, + Runnable::run); + + eventManager.start(); + + final UUID accountIdentifier = UUID.randomUUID(); + final byte deviceId = Device.PRIMARY_ID; + + assertDoesNotThrow(() -> eventManager.handleClientConnected(accountIdentifier, deviceId, new MessageAvailabilityAdapter()) + .toCompletableFuture() + .join()); + + assertTrue(eventManager.isLocallyPresent(accountIdentifier, deviceId)); + } + + private static RedisAdvancedClusterAsyncCommands buildBinaryAsyncCommands() { + @SuppressWarnings("unchecked") final RedisAdvancedClusterAsyncCommands binaryAsyncCommands = + mock(RedisAdvancedClusterAsyncCommands.class); + + when(binaryAsyncCommands.spublish(any(), any())).thenReturn(MockRedisFuture.completedFuture(0L)); + + return binaryAsyncCommands; + } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisherTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisherTest.java index 62c9a2afa..5746ee4ab 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisherTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/RedisDynamoDbMessagePublisherTest.java @@ -361,6 +361,26 @@ class RedisDynamoDbMessagePublisherTest { verify(redisMessageAvailabilityManager, timeout(1_000)).handleClientDisconnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher); } + @Test + void publishMessagesClientConnectedFailure() { + final CompletableFuture clientConnectedFuture = new CompletableFuture<>(); + + final RuntimeException exception = new RuntimeException("OH NO"); + clientConnectedFuture.completeExceptionally(exception); + + final RedisDynamoDbMessagePublisher messagePublisher = + new RedisDynamoDbMessagePublisher(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice, false); + + when(redisMessageAvailabilityManager.handleClientConnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher)) + .thenReturn(clientConnectedFuture); + + StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messagePublisher)) + .expectErrorMatches(exception::equals) + .verify(); + + verify(redisMessageAvailabilityManager).handleClientDisconnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher); + } + @ParameterizedTest @CsvSource({ "207, 173",