Skip to content

Commit b2352b8

Browse files
committed
fix: restore thread interrupt status after catching InterruptedException
This addresses issue #998 where Logback catches InterruptedException without re-interrupting the thread, potentially causing caller threads to lose their interruption status. Changes: - AbstractSocketAppender.append() and .connectSocketAndDispatchEvents() - ConcurrentServerRunner.run() - AsyncAppenderBase.Worker.run() - DefaultShutdownHook.run() - Example classes (ConsolePluginClient, NumberCruncherServer) Also clears any prior interrupt in AsyncAppenderBaseTest to ensure test stability. All existing tests pass.
1 parent 3ab9da9 commit b2352b8

7 files changed

Lines changed: 8 additions & 0 deletions

File tree

logback-core/src/main/java/ch/qos/logback/core/AsyncAppenderBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public void run() {
302302
aai.appendLoopOnAppenders(e);
303303
}
304304
} catch (InterruptedException e1) {
305+
Thread.currentThread().interrupt();
305306
// exit if interrupted
306307
break;
307308
}

logback-core/src/main/java/ch/qos/logback/core/hook/DefaultShutdownHook.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void run() {
6262
try {
6363
Thread.sleep(delay.getMilliseconds());
6464
} catch (InterruptedException e) {
65+
Thread.currentThread().interrupt();
6566
}
6667
}
6768
super.stop();

logback-core/src/main/java/ch/qos/logback/core/net/AbstractSocketAppender.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ protected void append(E event) {
188188
addInfo("Dropping event due to timeout limit of [" + eventDelayLimit + "] being exceeded");
189189
}
190190
} catch (InterruptedException e) {
191+
Thread.currentThread().interrupt();
191192
addError("Interrupted while appending event to SocketAppender", e);
192193
}
193194
}
@@ -211,6 +212,7 @@ private void connectSocketAndDispatchEvents() {
211212
}
212213
}
213214
} catch (InterruptedException ex) {
215+
Thread.currentThread().interrupt();
214216
assert true; // ok... we'll exit now
215217
}
216218
addInfo("shutting down");

logback-core/src/main/java/ch/qos/logback/core/net/server/ConcurrentServerRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void run() {
145145
}
146146
}
147147
} catch (InterruptedException ex) {
148+
Thread.currentThread().interrupt();
148149
assert true; // ok... we'll shut down
149150
} catch (Exception ex) {
150151
addError("listener: " + ex);

logback-core/src/test/java/ch/qos/logback/core/AsyncAppenderBaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public void workerThreadFlushesOnStop() throws InterruptedException {
220220
@Test
221221
public void stopExitsWhenMaxRuntimeReached() throws InterruptedException {
222222
int maxFlushTime = 1; // runtime of 0 means wait forever, so use 1 ms instead
223+
Thread.interrupted(); // clear any prior interrupt to ensure test stability
223224
int loopLen = 10;
224225
ListAppender<Integer> la = delayingListAppender;
225226
asyncAppenderBase.addAppender(la);

logback-examples/src/main/java/chapters/appenders/socket/ConsolePluginClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void run() {
110110
try {
111111
Thread.sleep(SLEEP);
112112
} catch (InterruptedException e) {
113+
Thread.currentThread().interrupt();
113114
e.printStackTrace();
114115
}
115116
}

logback-examples/src/main/java/chapters/mdc/NumberCruncherServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static void delay(int millis) {
118118
try {
119119
Thread.sleep(millis);
120120
} catch (InterruptedException e) {
121+
Thread.currentThread().interrupt();
121122
}
122123
}
123124

0 commit comments

Comments
 (0)