1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-21 19:42:05 +01:00

[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

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();