Copilot commented on code in PR #2262:
URL: https://github.com/apache/pekko/pull/2262#discussion_r2369383371


##########
stream/src/main/scala/org/apache/pekko/stream/scaladsl/Hub.scala:
##########
@@ -668,18 +671,16 @@ private[pekko] class 
BroadcastHub[T](startAfterNrOfConsumers: Int, bufferSize: I
     private def findAndRemoveConsumer(id: Long, offset: Int): Consumer = {
       // TODO: Try to eliminate modulo division somehow...
       val wheelSlot = offset & WheelMask
-      var consumersInSlot = consumerWheel(wheelSlot)
-      // debug(s"consumers before removal $consumersInSlot")
-      var remainingConsumersInSlot: List[Consumer] = Nil
+      val consumersInSlot = consumerWheel(wheelSlot)
       var removedConsumer: Consumer = null
-
-      while (consumersInSlot.nonEmpty) {
-        val consumer = consumersInSlot.head
-        if (consumer.id != id) remainingConsumersInSlot = consumer :: 
remainingConsumersInSlot
-        else removedConsumer = consumer
-        consumersInSlot = consumersInSlot.tail
+      if (consumersInSlot.size() > 0) {
+        consumersInSlot.removeIf(consumer => {
+          if (consumer.id == id) {
+            removedConsumer = consumer
+            true
+          } else false
+        })

Review Comment:
   The `removeIf` method with side effects (setting `removedConsumer`) creates 
a potential race condition. The predicate function should be pure. Consider 
using `iterator().remove()` pattern or finding the consumer first, then 
removing it separately.
   ```suggestion
           val it = consumersInSlot.iterator()
           while (it.hasNext && removedConsumer == null) {
             val consumer = it.next()
             if (consumer.id == id) {
               removedConsumer = consumer
               it.remove()
             }
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to