From 15a8da3eefa7f49efe4afbe8a50bc22b991dc58b Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 25 Dec 2023 17:52:48 +0100 Subject: [PATCH] [async] Fix busy-loop in reactor This is the same fix as I applied to Smack's reactor in 390f6f0fa788 ("[core] Fix busy-loop in SmackReactor") [1]. 1: https://github.com/igniterealtime/Smack/commit/390f6f0fa788a2bb386d3e6d62d5189763020c8a --- .../org/minidns/source/async/AsyncNetworkDataSource.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/minidns-async/src/main/java/org/minidns/source/async/AsyncNetworkDataSource.java b/minidns-async/src/main/java/org/minidns/source/async/AsyncNetworkDataSource.java index b14c3374..17890beb 100644 --- a/minidns-async/src/main/java/org/minidns/source/async/AsyncNetworkDataSource.java +++ b/minidns-async/src/main/java/org/minidns/source/async/AsyncNetworkDataSource.java @@ -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 requests = new ArrayList<>(myRequestsCount); for (int i = 0; i < myRequestsCount; i++) { AsyncDnsRequest asyncDnsRequest = INCOMING_REQUESTS.poll();