CASSANDRA-13159: Coalescing strategy can enter infinite loop

Patch by Corentin Chary; Reviewed by Jeff Jirsa for CASSANDRA-13159
diff --git a/CHANGES.txt b/CHANGES.txt
index 0c7d129..5c86687 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.1.17
+ * Coalescing strategy can enter infinite loop (CASSANDRA-13159)
  * Upgrade netty version to fix memory leak with client encryption (CASSANDRA-13114)
  * Fix paging for DISTINCT queries on partition keys and static columns (CASSANDRA-13017)
  * Fix race causing infinite loop if Thrift server is stopped before it starts listening (CASSANDRA-12856)
diff --git a/src/java/org/apache/cassandra/utils/CoalescingStrategies.java b/src/java/org/apache/cassandra/utils/CoalescingStrategies.java
index ca1399b..6b69066 100644
--- a/src/java/org/apache/cassandra/utils/CoalescingStrategies.java
+++ b/src/java/org/apache/cassandra/utils/CoalescingStrategies.java
@@ -96,7 +96,7 @@
     {
         // only sleep if we can expect to double the number of messages we're sending in the time interval
         long sleep = messages * averageGap;
-        if (sleep > maxCoalesceWindow)
+        if (sleep <= 0 || sleep > maxCoalesceWindow)
             return false;
 
         // assume we receive as many messages as we expect; apply the same logic to the future batch: