You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Michael Meß <mi...@macd.com> on 2012/07/31 15:38:28 UTC

Bug: LOG4J logging does not work correctly on Solaris when the interrupted flag of the Thread is set

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have found an issue on Solaris when the interrupted flag of the Java Thread which wants to log something is set:

Under Linux everything works well, but under Solaris, logging output gets corrupted.

On an earlier Log4J version we also had the issue that the interrupted flag got cleared unexpectedly when the Thread was logging something and has been interrupted just before logging.
The test shows that it is not the case now, when the interrupted flag was set before calling the logger, but I haven't tested yet, what happens, when the Thread gets interrupted while it is already executing code from Log4J.
Linux was not affected.

log4j   version = 1.2.17
Solaris version = SunOS xsola07 5.10 Generic_147441-12 i86pc i386 i86pc
                  Solaris 10 10/08 s10x_u6wos_07b X86

The Logger class in this example delegates the logging to LOG4J.

NOTE: In my case the UnitTest succeeds, but logging is corrupted. Please verify the logging output.

public class UnitTest_Logger {

    /**
     * This test tests the handling of the interrupted flag in Log4J. The interrupted flag should not be modified during
     * logging. DEVTS-353: Test if "Failed to flush writer" issue has been fixed: Should fail on Solaris, if the bug has
     * not been fixed. Hudson should complain on Solaris while this works under LINUX.
     *
     * @throws Exception
     */
    @Test
    public void testLoggingWhenInterrupted()
        throws Exception {
        boolean failed = false;
        final String longString = "...";
        int counter = 0;
        final Logger myLogger = new Logger( getClass() );
        final List<String> logMessageList = new ArrayList<String>();
        Thread.interrupted(); // Clear interrupted Flag (if it was set)
        for ( int i = 1; i <= 9; i++ ) {
            final boolean interrupted = getInterrupted();
            failed |= interrupted;
            final String logmessage = ++counter + " " + interruptedString( interrupted ) + i + longString;
            logMessageList.add( logmessage );
            myLogger.logInfo( logmessage );
        }
        Thread.currentThread().interrupt(); // Set interrupted Flag.
        for ( int i = 1; i <= 9; i++ ) {
            final boolean interrupted = getInterrupted();
            failed |= ( !interrupted );
            final String logmessage = ++counter + " " + interruptedString( interrupted ) + i + longString;
            logMessageList.add( logmessage );
            myLogger.logInfo( logmessage );
        }
        Thread.interrupted(); // Clear interrupted Flag
        for ( int i = 1; i <= 9; i++ ) {
            final boolean interrupted = getInterrupted();
            failed |= interrupted;
            final String logmessage = ++counter + " " + interruptedString( interrupted ) + i + longString;
            logMessageList.add( logmessage );
            myLogger.logInfo( logmessage );
        }
        assertFalse( "Interrupted flag has been modified unexpectedly.", failed );
        myLogger.logInfo( "These messages should have been logged:" );
        for ( final String logMessage : logMessageList ) {
            myLogger.logInfo( "" + logMessage );
        }
    }



    private String interruptedString( final boolean interrupted ) {
        return interrupted ? "INTERRUPTED" : "NOT interrupted";
    }



    private boolean getInterrupted() {
        return Thread.currentThread().isInterrupted();
    }



    public static void main( final String[] args ) {
        try {
            new UnitTest_Logger().testLoggingWhenInterrupted();
        } catch ( final Exception e ) {
            throw new IllegalStateException( e );
        }
    }

}



This is the logging output from Solaris: When the Interrupted flag is set, only one line gets logged, the rest is silently dropped.
After clearing the interrupted flag, everything works fine again.

[2012-07-31 14:43:03,877] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 1 NOT interrupted1...
[2012-07-31 14:43:03,877] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 2 NOT interrupted2...
[2012-07-31 14:43:03,877] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 3 NOT interrupted3...
[2012-07-31 14:43:03,878] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 4 NOT interrupted4...
[2012-07-31 14:43:03,878] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 5 NOT interrupted5...
[2012-07-31 14:43:03,878] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 6 NOT interrupted6...
[2012-07-31 14:43:03,878] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 7 NOT interrupted7...
[2012-07-31 14:43:03,878] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 8 NOT interrupted8...
[2012-07-31 14:43:03,879] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 9 NOT interrupted9...
[2012-07-31 14:43:03,879] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 10 INTERRUPTED1...
3,0001,3,0001,[2012-07-31 14:43:03,882] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 19 NOT interrupted1...
[2012-07-31 14:43:03,882] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 20 NOT interrupted2...
[2012-07-31 14:43:03,882] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 21 NOT interrupted3...
[2012-07-31 14:43:03,882] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 22 NOT interrupted4...
[2012-07-31 14:43:03,883] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 23 NOT interrupted5...
[2012-07-31 14:43:03,883] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 24 NOT interrupted6...
[2012-07-31 14:43:03,883] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 25 NOT interrupted7...
[2012-07-31 14:43:03,883] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 26 NOT interrupted8...
[2012-07-31 14:43:03,884] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 27 NOT interrupted9...
[2012-07-31 14:43:03,884] [INFO ] [com.macd.logging.UnitTest_Logger] [main] These messages should have been logged:
[2012-07-31 14:43:03,884] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 1 NOT interrupted1...
[2012-07-31 14:43:03,884] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 2 NOT interrupted2...
[2012-07-31 14:43:03,885] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 3 NOT interrupted3...
[2012-07-31 14:43:03,885] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 4 NOT interrupted4...
[2012-07-31 14:43:03,885] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 5 NOT interrupted5...
[2012-07-31 14:43:03,885] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 6 NOT interrupted6...
[2012-07-31 14:43:03,885] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 7 NOT interrupted7...
[2012-07-31 14:43:03,886] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 8 NOT interrupted8...
[2012-07-31 14:43:03,886] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 9 NOT interrupted9...
[2012-07-31 14:43:03,886] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 10 INTERRUPTED1...
[2012-07-31 14:43:03,886] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 11 INTERRUPTED2...
[2012-07-31 14:43:03,886] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 12 INTERRUPTED3...
[2012-07-31 14:43:03,887] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 13 INTERRUPTED4...
[2012-07-31 14:43:03,887] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 14 INTERRUPTED5...
[2012-07-31 14:43:03,887] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 15 INTERRUPTED6...
[2012-07-31 14:43:03,887] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 16 INTERRUPTED7...
[2012-07-31 14:43:03,888] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 17 INTERRUPTED8...
[2012-07-31 14:43:03,888] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 18 INTERRUPTED9...
[2012-07-31 14:43:03,888] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 19 NOT interrupted1...
[2012-07-31 14:43:03,888] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 20 NOT interrupted2...
[2012-07-31 14:43:03,888] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 21 NOT interrupted3...
[2012-07-31 14:43:03,889] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 22 NOT interrupted4...
[2012-07-31 14:43:03,889] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 23 NOT interrupted5...
[2012-07-31 14:43:03,889] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 24 NOT interrupted6...
[2012-07-31 14:43:03,889] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 25 NOT interrupted7...
[2012-07-31 14:43:03,890] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 26 NOT interrupted8...
[2012-07-31 14:43:03,890] [INFO ] [com.macd.logging.UnitTest_Logger] [main] 27 NOT interrupted9...

Cheers, Michael
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlAX39QACgkQz0gtiIh3JxFRvwCgrvRZuNSyFMwX51Fxp+2UVuy7
ab4AoJFPoi2TmLIkaOxRnhujfVAbF/qY
=9ud7
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org