From c247b8f6a1729a0ca969d0618c4b2e26dded65a9 Mon Sep 17 00:00:00 2001
From: bluegeek9 <steveneayers@gmail.com>
Date: Mon, 17 Feb 2025 11:25:06 -0600
Subject: [PATCH] Issue #3086125 by bluegeek9: Prevent infinite loop when
 sending messages with queue

---
 src/Subscribers.php | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/Subscribers.php b/src/Subscribers.php
index 7fbaa68..a85623e 100644
--- a/src/Subscribers.php
+++ b/src/Subscribers.php
@@ -177,9 +177,13 @@ class Subscribers implements SubscribersInterface {
     if ($subscribe_options['uids']) {
       // We got a list of user IDs directly from the implementing module,
       // However we need to adhere to the range.
-      $uids = $subscribe_options['range'] ? array_slice($subscribe_options['uids'], 0, $subscribe_options['range'], TRUE) : $subscribe_options['uids'];
-    }
+      $offset = 0;
+      if ($subscribe_options['last uid']) {
+        $offset = array_search($subscribe_options['last uid'], array_keys($subscribe_options['uids'])) + 1;
+      }
 
+      $uids = $subscribe_options['range'] ? array_slice($subscribe_options['uids'], $offset, $subscribe_options['range'], TRUE) : $subscribe_options['uids'];
+    }
     if (empty($uids) && !$uids = $this->getSubscribers($entity, $message, $subscribe_options, $context)) {
       // If we use a queue, it will be deleted.
       return;
@@ -231,7 +235,15 @@ class Subscribers implements SubscribersInterface {
       }
     }
 
-    if ($use_queue) {
+    $last_key = key(array_slice($subscribe_options['uids'], -1, 1, TRUE));
+
+    // Last key could not be found which means there are no more queue items to
+    // create.
+    if ($last_key === NULL) {
+      return;
+    }
+
+    if ($use_queue && isset($last_uid) && $last_key != $last_uid) {
       // Add item to the queue.
       $task = [
         'message' => $message,
-- 
GitLab