Bug 41219

Summary: Stacktraces of exceptions disappear occsionally
Product: Log4j - Now in Jira Reporter: Kari Ikonen <mr.kari.ikonen>
Component: AppenderAssignee: log4j-dev <log4j-dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 1.2   
Target Milestone: ---   
Hardware: Other   
OS: other   
Bug Depends on:    
Bug Blocks: 40951    

Description Kari Ikonen 2006-12-20 07:30:15 UTC
Sometimes stacktraces of the exceptions seem to mysteriously disappear. After 
invesigation I noticed thatThrowableInformation.VectorWriter looks potentially 
unrobust, since it's making strong assumptions of what methods of the 
PrintWriter API Throwable is using for printing its' stacktrace.

For example, if Throwable uses some "write(int)" method then stacktrace goes 
appearently into /dev/null.

It's completely possible that some sub-class of Throwable overrides 
printStackTrace() -logic, so API shouldn't make assumptions based into 
implementation details inside Throwable (Throwable itself could change also 
logic, like has happened earlier).

This issue reduces reliability of Log4J considerably.

PROOF:
---------------------
package org.kari;

import java.io.PrintWriter;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class ThrowableTest {
    
    public static final class ThrowableFail extends Throwable {

        @Override
        public void printStackTrace(PrintWriter s)
        {
            super.printStackTrace(s);
            s.println("LOST-START");
            // Following output is lost
            for (int i= 0; i < 100; i++) {
                s.print('a');
            }
            s.println("LOST-END");
        }
        
    }
    
    public static void main(String[] args) {
        BasicConfigurator.configure();
        Logger LOG = Logger.getLogger(ThrowableTest.class);
        LOG.info("Test", new ThrowableFail());
    }

}
---------------------
Comment 1 Kari Ikonen 2006-12-20 08:07:24 UTC
Related:
- Bug 34945 ThrowableInformation has dubious Stack Trace extraction feature
- Bug 35324 Stacktrace may choke on null fields, as 
  org.apache.log4j.spi.ThrowableInformation.VectorWriter.println(null) will bomb
- Bug 36587 Printing throwable stacktrace throwing null pointer exception
Comment 2 Henri Yandell 2007-01-25 17:23:23 UTC
I get the same when I run Kari's code against trunk.
Comment 3 Henri Yandell 2007-01-29 14:40:55 UTC
One simple improvement would be to ensure that all the methods in PrintWriter
are implemented by VectorWriter. I don't see why print(int) etc can't be
implemented - it looks like they've been there since 1.1.

JDK 1.5 added a bunch of methods to VectorWriter, so if Exceptions use these
then the same bug will exist.
Comment 4 Kari Ikonen 2007-01-29 21:01:55 UTC
Basically, only lowest level write methods should be overridden, i.e. the ones 
into which all the other calls end up. This means overriding only "public void 
write(int c)" in the Writer, which is passed as constructor arg to PrintWriter 
(thus changing logic a bit). And division into lines would happens then by 
detecting linefeed.
Comment 5 Curt Arnold 2007-02-21 13:43:16 UTC
Fixes committed against 1.2 branch in rev 510214 and against trunk in 510242.

Basically gutted the existing VectorWriter/NullWriter approach and passed a PrintWriter wrapping a 
StringWriter to printStackTrace and then used LineNumberReader to parse the output into lines.