FULL PRODUCT VERSION :
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Windows XP Professional - Version 2002 - Service Pack 2
A DESCRIPTION OF THE PROBLEM :
Throwable class is not thread safe regarding private StackTraceElement[] stackTrace field.
Can be modified concurrently by two threads causing ArrayIndexOutOfBoundsException.
Two places of modification:
1) setStackTrace
public void setStackTrace(StackTraceElement[] stackTrace) {
....
this.stackTrace = defensiveCopy;
}
2) getOurStackTrace
private synchronized StackTraceElement[] getOurStackTrace() {
// Initialize stack trace if this is the first call to this method
if (stackTrace == null) {
int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
for (int i=0; i < depth; i++)
stackTrace[i] = getStackTraceElement(i); // throws ArrayIndexOutOfBoundsException
}
return stackTrace;
}
Solution: setStackTrace should be synchronized as well
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run two threads calling on the same Throwable object:
1) first thread - setStackTrace
2) second thread - getStackTrace
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Not exception thrown
ACTUAL -
Occasional ArrayIndexOutOfBoundsException
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.ArrayIndexOutOfBoundsException: 65
at java.lang.Throwable.getOurStackTrace(Throwable.java:592)
at java.lang.Throwable.printStackTrace(Throwable.java:511)
at org.apache.log4j.spi.ThrowableInformation.getThrowableStrRep(ThrowableInformation.java:59)
at org.apache.log4j.spi.LoggingEvent.getThrowableStrRep(LoggingEvent.java:342)
at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:304)
at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:236)
at org.apache.log4j.WriterAppender.append(WriterAppender.java:159)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:230)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:65)
at org.apache.log4j.Category.callAppenders(Category.java:203)
at org.apache.log4j.Category.forcedLog(Category.java:388)
at org.apache.log4j.Category.log(Category.java:823)
REPRODUCIBILITY :
This bug can be reproduced occasionally.