Skip to content

Commit

Permalink
[async] Fix busy-loop in reactor
Browse files Browse the repository at this point in the history
This is the same fix as I applied to Smack's reactor in
390f6f0fa788 ("[core] Fix busy-loop in SmackReactor") [1].

1: igniterealtime/Smack@390f6f0
  • Loading branch information
Flowdalic committed Dec 25, 2023
1 parent 2cd9c3c commit 15a8da3
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ private static void handleIncomingRequests() {
}

int myRequestsCount = incomingRequestsSize / REACTOR_THREAD_COUNT;
// The division could result in myRequestCount being zero despite pending incoming
// requests. Therefore, ensure this thread tries to get at least one incoming
// request by invoking poll(). Otherwise, we might end up in a busy loop
// where myRequestCount is zero, and this thread invokes a selector.wakeup() below
// because incomingRequestsSize is not empty, but the woken-up reactor thread
// will end up with myRequestCount being zero again, restarting the busy-loop cycle.
if (myRequestsCount == 0) myRequestsCount = 1;
Collection<AsyncDnsRequest> requests = new ArrayList<>(myRequestsCount);
for (int i = 0; i < myRequestsCount; i++) {
AsyncDnsRequest asyncDnsRequest = INCOMING_REQUESTS.poll();
Expand Down

0 comments on commit 15a8da3

Please sign in to comment.