[core] Fix busy-loop in SmackReactor

Fixes SMACK-938.
This commit is contained in:
Florian Schmaus 2023-12-07 11:17:32 +01:00
parent 0fb8bfdf6c
commit 390f6f0fa7
1 changed files with 6 additions and 0 deletions

View File

@ -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<SelectionKey> selectedKeys = new ArrayList<>(myKeyCount);
for (int i = 0; i < myKeyCount; i++) {
SelectionKey selectionKey = pendingSelectionKeys.poll();