Details
-
Bug
-
Resolution: Unresolved
-
Major
-
None
-
1.1.7
-
None
-
Linux, Java 8
Description
In my project I have a UI thread and some number of worker threads which can be interrupted by an user clicking a button in UI.
Recently I noticed that worker threads are in fact not always interrupted. Doing some digging I found that in my case the problem was in AbstractSocketAppender's append() method, where interrupted exception gets ignored:
// Some comments here try { final boolean inserted = deque.offer(event, eventDelayLimit.getMilliseconds(), TimeUnit.MILLISECONDS); if (!inserted) { addInfo("Dropping event due to timeout limit of [" + eventDelayLimit + "] being exceeded"); } } catch (InterruptedException e) { addError("Interrupted while appending event to SocketAppender", e); }
Here is the little example that exposes the issue (make sure that SocketAppender is enabled in logback.xml). The program starts another thread that constantly writes to a logger. The main thread waits for 3 seconds and then tries to interrupt the other thread. This never happens.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LoggerChecker { private static final Logger LOGGER = LoggerFactory.getLogger(LoggerChecker.class); public static void main (String[] args) throws Exception { Thread t = new Thread(() -> { try { while (true) { LOGGER.debug("Nothing"); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } } } catch (Exception e) { e.printStackTrace(); } }); t.start(); Thread.sleep(3000); t.interrupt(); } }