From 390f6f0fa788a2bb386d3e6d62d5189763020c8a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 7 Dec 2023 11:17:32 +0100 Subject: [PATCH] [core] Fix busy-loop in SmackReactor Fixes SMACK-938. --- .../src/main/java/org/jivesoftware/smack/SmackReactor.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackReactor.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackReactor.java index 67e68f1fd..227b3a2e8 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackReactor.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackReactor.java @@ -327,6 +327,12 @@ public class SmackReactor { int currentReactorThreadCount = reactorThreads.size(); int myKeyCount = pendingSelectionKeysSize / currentReactorThreadCount; + // The division could result in myKeyCount being zero, even though there are pending selection keys. + // Therefore, ensure that this thread tries to get at least one pending selection key by invoking poll(). + // Otherwise, it could happen that we end up in a busy loop, where myKeyCount is zero and this thread invokes + // selector.wakeup() below because pendingSelectionsKeys is not empty, but the woken up reactor thread wil + // end up with myKeyCount being zero again, restarting the busy-loop cycle. + if (myKeyCount == 0) myKeyCount = 1; Collection selectedKeys = new ArrayList<>(myKeyCount); for (int i = 0; i < myKeyCount; i++) { SelectionKey selectionKey = pendingSelectionKeys.poll();