Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6429910

(fc) FileChannel.lock() IOException: Bad file number, not AsynchronousCloseException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 1.4.2
    • core-libs
    • b57
    • sparc
    • solaris_8

        FULL PRODUCT VERSION :
        Both:

        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
        Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
        Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        SunOS goldtpus7 5.8 Generic_108528-23 sun4u sparc SUNW,Sun-Fire-280R

        A DESCRIPTION OF THE PROBLEM :
        Coding timeout for FileChannel.lock() by closing the channel from a timer thread. Get

        java.io.IOException: Bad file number
                at sun.nio.ch.FileChannelImpl.lock0(Native Method)
                at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:750)
                at java.nio.channels.FileChannel.lock(FileChannel.java:865)

        Instead of AsynchronousCloseException

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the applied test.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        test passed
        ACTUAL -
        test failed: wrong exception class
        java.io.IOException: Bad file number
                at sun.nio.ch.FileChannelImpl.lock0(Native Method)
                at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:750)
                at java.nio.channels.FileChannel.lock(FileChannel.java:865)
                at Simul$Waiter.run(Simul.java:14)
                at java.lang.Thread.run(Thread.java:534)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.io.*;
        import java.nio.channels.*;

        public class Simul
        {
            static FileChannel m_channel;

            static class Waiter implements Runnable
            {
                public void run()
                {
                    try
                    {
                        m_channel.lock();
                    }
                    catch( AsynchronousCloseException ace )
                    {
                        System.out.println( "test passed" );
                        return;
                    }
                    catch( IOException ioe )
                    {
                        System.out.println( "test failed: wrong exception class" );
                        ioe.printStackTrace();
                        return;
                    }
                    System.out.println( "test failed\nwe were blocked until " +
                            "the other jvm released the lock\n" +
                            "are we on windows? this test was for Solaris!" );
                }
            }
            public static void main( final String[] args )
            {
                try
                {
                    if ( args.length > 0 )
                    {
                        /* we just lock the file for 5 seconds */
                        final FileOutputStream fos = new FileOutputStream( args[0] );
                        final FileChannel channel = fos.getChannel();
                        channel.lock();
                        Thread.sleep( 5000l );
                    }
                    else
                    {
                        final File currDir = new File( "." );
                        final File playFile =
                                File.createTempFile( "aaa_", ".txt", currDir );

                        final String playPath = playFile.getCanonicalPath();
                        final Runtime runtime = Runtime.getRuntime();
                        final String execArgs[] = new String[]
                        {
                            "java",
                            "-cp",
                            ".",
                            Simul.class.getName(),
                            playPath,
                        };

                        final Process me2 = runtime.exec( execArgs );

                        /* give the other jvm time to start up */
                        Thread.sleep( 1000l );
                        try
                        {
                            me2.exitValue();
                            System.out.println( "failed to launch second jvm" );
                        }
                        catch( IllegalThreadStateException itse )
                        {
                            final FileOutputStream fos = new FileOutputStream( playFile );
                            m_channel = fos.getChannel();
                            final Runnable waiter = new Waiter();
                            final Thread t = new Thread( waiter, "waiter" );
                            t.start();
                            /* give the waiter time to start */
                            Thread.sleep( 500l );
                            fos.close();
                            m_channel.close();
                        }
                    }
                }
                catch( Throwable t )
                {
                    t.printStackTrace();
                }
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Catch IOException
        and treat it as
        AsynchronousCloseException

              alanb Alan Bateman
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: