Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(sqs): release in-flight ReceiveMessage long polls when their queue is deleted (#1286)

TomDav2891f8af70627d
diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java
index c988f5ec05..c6ff7fb958 100644
--- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java
@@ -277,6 +277,11 @@ public class SqsService {
if (dedupStore != null) {
dedupStore.delete(storageKey);
}
+ // Wake parked ReceiveMessage long polls so they observe the deletion
+ // and finish, instead of staying registered against this queue URL.
+ // Left alone they would survive a delete + recreate under the same URL
+ // and consume deliveries that belong to the new queue's consumers.
+ notifyReceivers(storageKey);
LOG.infov("Deleted queue: {0}", queueUrl);
}
@@ -592,8 +597,20 @@ public class SqsService {
long start = System.currentTimeMillis();
long maxWait = waitTimeSeconds * 1000L;
Object lock = queueLocks.computeIfAbsent(storageKey, k -> new Object());
+ // The queue incarnation this call polls against. DeleteQueue removes it
+ // from messagesByQueue (and CreateQueue registers a fresh instance), so
+ // an identity change tells a parked long poll that its queue is gone.
+ GuardedMessageQueue polledQueue = getOrCreateQueue(storageKey);
while (true) {
+ if (messagesByQueue.get(storageKey) != polledQueue) {
+ // The queue was deleted mid-poll (and possibly recreated under
+ // the same URL). Finish empty-handed: a receive opened against
+ // the deleted incarnation must not consume deliveries — nor
+ // burn ApproximateReceiveCount / redrive budget — that belong
+ // to the new queue's consumers.
+ return Collections.emptyList();
+ }
List<Message> result = doReceiveMessage(storageKey, maxMessages, visibilityTimeout, region);
if (!result.isEmpty() || maxWait <= 0) {
if (!result.isEmpty() && LOG.isTraceEnabled()) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java
index 679f4a3432..87460f6d62 100644
--- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java
@@ -12,6 +12,9 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
@@ -945,4 +948,69 @@ class SqsServiceTest {
Map<String, String> attrs = sqsService.getQueueAttributes("attrs-bare", List.of("VisibilityTimeout"), region);
assertEquals("45", attrs.get("VisibilityTimeout"));
}
+
+ // --- DeleteQueue releases in-flight ReceiveMessage long polls ---
+
+ @Test
+ void deleteQueueReleasesParkedLongPoll() throws InterruptedException {
+ String region = "eu-west-1";
+ Queue queue = sqsService.createQueue("longpoll-release-queue", null, region);
+ String queueUrl = queue.getQueueUrl();
+
+ final var pollerEntered = new CountDownLatch(1);
+ final var staleResult = new AtomicReference<List<Message>>();
+ Thread stalePoller = new Thread(() -> {
+ pollerEntered.countDown();
+ staleResult.set(sqsService.receiveMessage(queueUrl, 1, 30, 8, region));
+ });
+ stalePoller.start();
+ assertTrue(pollerEntered.await(5, TimeUnit.SECONDS));
+ Thread.sleep(300); // let the poller park inside its long-poll wait
+
+ sqsService.deleteQueue(queueUrl, region);
+ stalePoller.join(2000);
+
+ assertFalse(stalePoller.isAlive(),
+ "DeleteQueue must release a parked long poll promptly, not leave it to wait out its full WaitTimeSeconds");
+ assertTrue(staleResult.get().isEmpty(),
+ "A long poll released by DeleteQueue must complete without messages");
+ }
+
+ @Test
+ void staleLongPollMustNotConsumeMessagesFromARecreatedQueue() throws InterruptedException {
+ String region = "eu-west-1";
+ Queue queue = sqsService.createQueue("recreate-longpoll-queue", null, region);
+ String queueUrl = queue.getQueueUrl();
+
+ // A long poll opened before the delete, e.g. a listener generation
+ // that is being torn down while its queue is recreated.
+ final var pollerEntered = new CountDownLatch(1);
+ final var staleResult = new AtomicReference<List<Message>>();
+ Thread stalePoller = new Thread(() -> {
+ pollerEntered.countDown();
+ staleResult.set(sqsService.receiveMessage(queueUrl, 1, 30, 8, region));
+ });
+ stalePoller.start();
+ assertTrue(pollerEntered.await(5, TimeUnit.SECONDS));
+ Thread.sleep(300); // let the poller park inside its long-poll wait
+
+ sqsService.deleteQueue(queueUrl, region);
+ Queue recreated = sqsService.createQueue("recreate-longpoll-queue", null, region);
+ sqsService.sendMessage(recreated.getQueueUrl(), "for-the-new-queue", 0, region);
+ // Give a surviving stale poll every chance to (incorrectly) grab the
+ // message before the legitimate receive arrives.
+ Thread.sleep(200);
+
+ List<Message> fresh = sqsService.receiveMessage(recreated.getQueueUrl(), 1, 30, 1, region);
+ stalePoller.join(9000);
+ assertFalse(stalePoller.isAlive());
+
+ assertEquals(1, fresh.size(),
+ "A receive opened after the recreate must get the new queue's message");
+ assertEquals("for-the-new-queue", fresh.get(0).getBody());
+ assertEquals(1, fresh.get(0).getReceiveCount(),
+ "The delivery to the live receiver must be the first receive (ApproximateReceiveCount = 1)");
+ assertTrue(staleResult.get().isEmpty(),
+ "The pre-delete long poll must not consume the recreated queue's delivery");
+ }
}