[logback-user] AsyncAppender dropping events when thread is interrupted

Jakob Bergendahl jakob at bergendahl.org
Mon Jan 23 16:18:16 CET 2017


Hi,

As mentioned in a comment on LOGBACK-910, the AsyncAppender will silently
drop events when the current thread is interrupted. The simplest test for
this is:

    Thread.currentThread().interrupt();
    log.warn("message 1"); // This was always dropped
    log.warn("message 2"); // This is also dropped since LOGBACK-910 was
fixed

I have recently spent a long time troubleshooting a case where an error was
sometimes not logged because of this. Have you considered using something
similar to Guava's Uninterruptibles.putUninterruptibly instead, i.e. try to
put in a loop while interrupted and reset the interrupt status once the put
succeeds? Code-wise this would mean changing AsyncAppenderBase from:

    private void put(E eventObject) {
        if (neverBlock) {
            blockingQueue.offer(eventObject);
        } else {
            try {
                blockingQueue.put(eventObject);
            } catch (InterruptedException e) {
                // Interruption of current thread when in doAppend method
should not be consumed
                // by AsyncAppender
        }
    }

to something like (assuming no Guava dependency):

    private void put(E eventObject) {
        if (neverBlock) {
            blockingQueue.offer(eventObject);
        } else {
            putUninterruptibly(eventObject);
        }
    }

    private void putUninterruptibly(E eventObject) {
        boolean interrupted = false;
        try {
            while (true) {
                try {
                    blockingQueue.put(eventObject);
                    break;
                } catch (InterruptedException e) {
                    interrupted = true;
                }
            }
        } finally {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }

Does this make sense? I would be willing to help out with this, but not
sure what my next step should be.

/Jakob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20170123/554c23e4/attachment.html>


More information about the logback-user mailing list