Skip to content

Commit ad35cd7

Browse files
committed
Handle interrupts on NIO selector thread
Closes #396
1 parent 9f0fd8b commit ad35cd7

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/main/java/org/xbill/DNS/NioClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static void close() {
9494
}
9595

9696
private static void close(boolean fromHook) {
97+
log.debug("Closing dnsjava NIO selector, fromHook={}", fromHook);
9798
run = false;
9899
Selector localSelector = selector;
99100
if (localSelector != null) {
@@ -139,7 +140,14 @@ static void runSelector() {
139140

140141
while (run) {
141142
try {
142-
if (selector.select(timeout) == 0) {
143+
int numSelects = selector.select(timeout);
144+
if (Thread.currentThread().isInterrupted()) {
145+
log.debug("Sector thread was interrupted, stopping");
146+
close();
147+
break;
148+
}
149+
150+
if (numSelects == 0) {
143151
runTasks(TIMEOUT_TASKS);
144152
}
145153

src/main/java/org/xbill/DNS/NioTcpClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ private void checkTransactionTimeouts() {
6666
private void closeTcp() {
6767
registrationQueue.clear();
6868
EOFException closing = new EOFException("Client is closing");
69-
channelMap.forEach(
70-
(key, state) -> {
71-
state.handleTransactionException(closing);
72-
state.handleChannelException(closing);
73-
});
69+
for (ChannelState state : channelMap.values()) {
70+
state.handleTransactionException(closing);
71+
state.handleChannelException(closing);
72+
}
7473
channelMap.clear();
7574
}
7675

0 commit comments

Comments
 (0)