Skip to content

ERROR Recursive call to appender Sentry #139

Closed

Description

@frecar

Hi, I'm trying to configure log4j2 and sentry to work using this config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" shutdownHook="disable"
               packages="org.apache.logging.log4j.core,net.kencochrane.raven.log4j2">

    <appenders>
        <Raven name="Sentry">
            <dsn>
                https://url-to-sentry
            </dsn>
            <ravenFactory>net.kencochrane.raven.DefaultRavenFactory</ravenFactory>
        </Raven>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </appenders>

    <loggers>
        <root level="all">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="Sentry"/>
        </root>
    </loggers>

</configuration>

But I keep getting "ERROR Recursive call to appender Sentry" printed

What am I doing wrong?

Activity

  1. suhyunjeon commented on Jul 7, 2015

    @suhyunjeon

    @frecar Did you solve the problem? I have same problem.

  2. tootedom commented on Sep 15, 2015

    @tootedom

    We're having a similar issue when using Logback except handily Logback doesn't tell us it's recursing.

    The problem is with the code in the EventBuilder on line 556 or line 562.

    Basically, when the hostname hasn't yet been set, the EventBuilder (called by SentryAppender) tries to update its hostname cache. This will always log a message (either DEBUG or WARN depending on the outcome of the get hostname call) which is therefore a recursive call. We're hitting this issue in Logback and it's a really big problem when the AsyncAppender's BlockingQueue is full (i.e. we're logging lots of stuff) because it locks our app completely. We can hit it pretty reliably if we turn the hostname cache time down to e.g. 100ms and cause our application to log frequently.

    We've discussed several solutions to this, none of which are particularly great. The easiest is to remove the lines linked above and never log from the EventBuilder when it's called from the SentryAppender.

    Here's an example stack trace of a locked thread encountering this issue:

    "AsyncAppender-Worker-Thread-2" #12 daemon prio=5 os_prio=31 tid=0x00007f9a3aaaf800 nid=0x5d03 waiting on condition [0x0000000124c5d000]
       java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000006c000ea00> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:353)
        at ch.qos.logback.core.AsyncAppenderBase.put(AsyncAppenderBase.java:139)
        at ch.qos.logback.core.AsyncAppenderBase.append(AsyncAppenderBase.java:130)
        at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)
        at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
        at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:273)
        at ch.qos.logback.classic.Logger.callAppenders(Logger.java:260)
        at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:442)
        at ch.qos.logback.classic.Logger.filterAndLog_2(Logger.java:433)
        at ch.qos.logback.classic.Logger.warn(Logger.java:721)
        at net.kencochrane.raven.event.EventBuilder$HostnameCache.updateCache(EventBuilder.java:564)
        at net.kencochrane.raven.event.EventBuilder$HostnameCache.getHostname(EventBuilder.java:543)
        at net.kencochrane.raven.event.EventBuilder.autoSetMissingValues(EventBuilder.java:87)
        at net.kencochrane.raven.event.EventBuilder.build(EventBuilder.java:297)
        - locked <0x00000007811fca90> (a net.kencochrane.raven.event.EventBuilder)
        at net.kencochrane.raven.logback.SentryAppender.buildEvent(SentryAppender.java:207)
        at net.kencochrane.raven.logback.SentryAppender.append(SentryAppender.java:132)
        at net.kencochrane.raven.logback.SentryAppender.append(SentryAppender.java:26)
        at ch.qos.logback.core.AppenderBase.doAppend(AppenderBase.java:85)
        - locked <0x00000006c000e130> (a net.kencochrane.raven.logback.SentryAppender)
        at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
        at ch.qos.logback.core.AsyncAppenderBase$Worker.run(AsyncAppenderBase.java:226)
    

    Note that if we change the SentryAppender to extend UnsynchronizedAppenderBase instead of AppenderBase (we thought that might fix the issue, sadly not) then we get the following stack trace which clearly shows two calls to UnsynchronizedAppenderBase.java:88:

    "AsyncAppender-Worker-Thread-2" #12 daemon prio=5 os_prio=31 tid=0x00007fb2be87b000 nid=0x5d03 waiting on condition [0x000000012039a000]
       java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000006c019c408> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:353)
        at ch.qos.logback.core.AsyncAppenderBase.put(AsyncAppenderBase.java:139)
        at ch.qos.logback.core.AsyncAppenderBase.append(AsyncAppenderBase.java:130)
        at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)
        at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
        at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:273)
        at ch.qos.logback.classic.Logger.callAppenders(Logger.java:260)
        at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:442)
        at ch.qos.logback.classic.Logger.filterAndLog_2(Logger.java:433)
        at ch.qos.logback.classic.Logger.warn(Logger.java:721)
        at net.kencochrane.raven.event.EventBuilder$HostnameCache.updateCache(EventBuilder.java:562)
        at net.kencochrane.raven.event.EventBuilder$HostnameCache.getHostname(EventBuilder.java:543)
        at net.kencochrane.raven.event.EventBuilder.autoSetMissingValues(EventBuilder.java:87)
        at net.kencochrane.raven.event.EventBuilder.build(EventBuilder.java:297)
        - locked <0x00000007767df740> (a net.kencochrane.raven.event.EventBuilder)
        at net.kencochrane.raven.logback.SentryAppender.buildEvent(SentryAppender.java:207)
        at net.kencochrane.raven.logback.SentryAppender.append(SentryAppender.java:132)
        at net.kencochrane.raven.logback.SentryAppender.append(SentryAppender.java:26)
        at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88)
        at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48)
        at ch.qos.logback.core.AsyncAppenderBase$Worker.run(AsyncAppenderBase.java:226)
    

    It looks like in Logback world, if you're logging from within an appender you have to call one of the addStatus methods like in this example, the line:

    addError("No encoder set for the appender named ["+ name +"].");

    Unfortunately as the DEBUG and WARN calls are in the Raven code and aren't Logback specific we can't create a generic solution to this.

    TL;DR This deadlocks our app when using Logback (which we're stuck with due to it being a dependency of DropWizard) and could be a very big issue. We think you should remove line 556 and line 562 of EventBuilder.

  3. tootedom commented on Sep 16, 2015

    @tootedom

    After further investigation, we discovered more calls to the logger, not just on lines 556 and 562 of EventBuilder. As such, we've implemented a filter so that any events from net.kencochrane.raven don't ever get sent to the SentryAppender, see tradier/dropwizard-raven#5

  4. loucasa commented on Oct 5, 2016

    @loucasa

    I have the same problem, it seems to happen when the sentry appender receives info events, i.e. I don't get the error when I have:

    <appenderref ref="Sentry" level="warn"/>

  5. bretthoerner commented on Oct 7, 2016

    @bretthoerner

    @loucasa Which logging implementation are you using?

  6. loucasa commented on Oct 11, 2016

    @loucasa

    Same, log4j2

  7. bretthoerner commented on Oct 13, 2016

    @bretthoerner

    What version of Raven? I'm trying to recreate the issue with the latest version but I'm having no luck.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration status="warn" packages="org.apache.logging.log4j.core,com.getsentry.raven.log4j2">
    
        <appenders>
            <Raven name="Sentry"></Raven>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </appenders>
    
        <loggers>
            <root level="debug">
                <appender-ref ref="Console"/>
                <appender-ref ref="Sentry"/>
            </root>
        </loggers>
    
    </configuration>
    package com.getsentry.example;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class Application
    {
        private static final Logger logger = LogManager.getLogger(Application.class);
    
        public static void main(String[] args)
        {
            logger.info("Test INFO");
    
            try {
                int example = 1 / 0;
            } catch (Exception e) {
                logger.error("Caught exception!", e);
            }
        }
    }
  8. bretthoerner commented on Oct 14, 2016

    @bretthoerner

    I'm hoping this helps: #265

  9. bretthoerner commented on Oct 15, 2016

    @bretthoerner

    The above (filtering out com.getsentry.raven from the SentryAppender) was released as part of 7.8.0 if you'd like to try it out.

  10. loucasa commented on Oct 17, 2016

    @loucasa

    @bretthoerner this did not fix the issue, stepping through the code I think I see the problem, on the first log event, the SentryAppender initialises raven using the DefaultRavenFactory but during initialisation it will attempt to log info here causing the recursive call.

    Maybe the reason you don't see it is because you have no <dsn> in your log4j config and so it does not enter any of the if conditions

  11. bretthoerner commented on Oct 17, 2016

    @bretthoerner

    I set my DSN externally with an environment variable, and am using an https connection, so that is weird. I see what you mean though (even though I can't seem to reproduce it).

  12. loucasa commented on Oct 17, 2016

    @loucasa

    my log4j version is 2.6.1 if it makes any difference

  13. bretthoerner commented on Oct 17, 2016

    @bretthoerner

    Thanks @loucasa that makes a lot more sense to me, I'll see about a fix.

  14. bretthoerner commented on Oct 28, 2016

    @bretthoerner

    For the life of me I can't reproduce this with log4j2 2.6.1. Logging set to all or info, DSN set to HTTP (I see Sentry INFO logging about the connection too). What is your slf4j version?

  15. loucasa commented on Oct 31, 2016

    @loucasa

    slf4j.version=1.7.21. I wonder if its the sequence of events, if you step though it, is the first log event from com.getsentry.raven.DefaultRavenFactory.createConnection() or your applications first log event? Mine is from the app. Another thing to check is whether anything in org.apache.logging.log4j.core.config.AppenderControl.shouldSkip() prevents the recursive call.

  16. bretthoerner commented on Feb 3, 2017

    @bretthoerner

    I tried chasing this down again to no avail. It's interesting that a user above fixed this in an older version by filtering out all Sentry logs from Sentry itself, which we do now for all logging frameworks.

    I've realized that our documentation and example logging configurations were pretty poor, because I don't understand why you'd actually want to set the Sentry appender to anything below WARN. Our example apparently told people to configure Sentry at ALL which is just a bad idea all around. But, most of all, I can't seem to reproduce this at all.

    That said, I'm going to improve the docs/examples so that Sentry is filtered to WARN and above -- and if this is an actual production issue for anyone I'd be curious on the details or an example mini project I can use to repro.

  17. darmbrust commented on Dec 16, 2019

    @darmbrust

    FYI, probably not related to this bug, but for anyone else that stumbles here from a google search for errors with recursive warnings from log4j:

    https://issues.apache.org/jira/browse/LOG4J2-2738

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions