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

(fc) FileChannel.tryLock leaves FileLock on lockList if I/O error occurs

    Details

    • Subcomponent:
    • Introduced In Version:
    • CPU:
      x86, sparc
    • OS:
      linux, solaris_10

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.5.0_08"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
        Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)


        ADDITIONAL OS VERSION INFORMATION :
        Linux 2.6.20-16-generic #2 SMP Sun Sep 23 19:50:39 UTC 2007 i686 GNU/Linux

        But probably applies to other OSs as well

        A DESCRIPTION OF THE PROBLEM :
        The semantics of lock() and tryLock() are different when the underlying lock cannot be obtained due to an IOException.

        lock() removes the lock from the lockList wheras tryLock() does not. One effect of this is that if tryLock() is called again, an OverlappingFileLockException is thrown instead of a more appropriate IOException.

        The bug can be clearly observed by looking at the two methods:

            public FileLock lock(long position, long size, boolean shared)
        throws IOException
            {
        ...
                checkList(position, size);
                addList(fli);
        ...
                try {
        ...
                } catch (IOException e) {
                    removeList(fli);
                    throw e;
                }
        ...
          }

           public FileLock tryLock(long position, long size, boolean shared)
        throws IOException
            {
        ...
                checkList(position, size);
                addList(fli);
        ...
            }

        The fix it to modify tryLock() to catch the IOException, call removeList(), and rethrow the IOException

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
            public static void main(String[] args) throws IOException {
                RandomAccessFile raf = new RandomAccessFile("/tmp/foo", "rw");
                FileChannel channel = raf.getChannel();
                
                try {
                    channel.lock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.lock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.tryLock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.tryLock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
            }



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        java.io.IOException: No locks available
        java.io.IOException: No locks available
        java.io.IOException: No locks available
        java.io.IOException: No locks available

        ACTUAL -
        java.io.IOException: No locks available
        java.io.IOException: No locks available
        java.io.IOException: No locks available
        java.nio.channels.OverlappingFileLockException

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.io.IOException;
        import java.io.RandomAccessFile;
        import java.nio.channels.FileChannel;

        public class FileLockBug {

            public static void main(String[] args) throws IOException {
                RandomAccessFile raf = new RandomAccessFile("/tmp/foo", "rw");
                FileChannel channel = raf.getChannel();
                
                try {
                    channel.lock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.lock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.tryLock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
                
                try {
                    channel.tryLock();
                }
                catch (Throwable t) {
                    System.out.println(t);
                }
            }
            
        }

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

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: