From 3a67d561b5e2320314eda9e3bb5d1ab89eccab89 Mon Sep 17 00:00:00 2001 From: Michael Dombrowski Date: Mon, 9 Oct 2023 04:26:51 -0400 Subject: [PATCH] Handle 0 sized collector batches in PostOffice (#777) This change is attempting to resolve #750 where the call to retain is 0 and therefore countBatches must have returned 0. I know that this change will absolutely avoid the error, however, I'm not clear on why the number of batches would be 0 while this code is running. In this change, I simply treat it exactly the same as the above case where there are no subscriptions (and thus no work to do). (cherry picked from commit 8ab1c91061f3350ab1143a2eb91fdbda2b18c8a5) --- .../src/main/java/io/moquette/broker/PostOffice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/broker/src/main/java/io/moquette/broker/PostOffice.java b/broker/src/main/java/io/moquette/broker/PostOffice.java index fa2631f2a..866b2d5a1 100644 --- a/broker/src/main/java/io/moquette/broker/PostOffice.java +++ b/broker/src/main/java/io/moquette/broker/PostOffice.java @@ -469,7 +469,15 @@ private RoutingResults publish2Subscribers(ByteBuf payload, Topic topic, MqttQoS collector.add(sub); } } - payload.retain(collector.countBatches()); + + int subscriptionCount = collector.countBatches(); + if (subscriptionCount <= 0) { + // no matching subscriptions, clean exit + LOG.trace("No matching subscriptions for topic: {}", topic); + return new RoutingResults(Collections.emptyList(), Collections.emptyList(), CompletableFuture.completedFuture(null)); + } + + payload.retain(subscriptionCount); List publishResults = collector.routeBatchedPublishes((batch) -> { publishToSession(payload, topic, batch, publishingQos);