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

File.mkdirs() fails due to race condition

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 1.3.0, 1.4.1, 5.0u11, 6
    • core-libs
    • b09
    • generic, x86
    • generic, windows_2000
    • Not verified

        Name: nt126004 Date: 09/05/2002


        FULL PRODUCT VERSION :
        java version "1.4.1-rc"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
        Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)

        FULL OPERATING SYSTEM VERSION :
        Microsoft Windows 2000 [Version 5.00.2195]

        ADDITIONAL OPERATING SYSTEMS :
        All others probably



        A DESCRIPTION OF THE PROBLEM :
        File.mkdirs fails to create the entire hierarchy of
        directories if another application or thread creates
        one of the intervening directories while mkdirs is
        running.

        An example: We have two threads, A and B.
        A calls new File("c:\base\a").mkdirs();
        B calls new File("c:\base\b").mkdirs();

        Pseudo-trace (follow along in File.mkdirs):
        A: if (exists()) // c:\base\a doesn't exist
        A: if (mkdir()) // fails to create c:\base\a
        A: parent = getParent(); // c:\base
        A: new File(parent).mkdirs()
        B: if (exists()) // c:\base\b doesn't exist
        B: if (mkdir()) // fails to create c:\base\b
        B: parent = getParent(); // c:\base
        B: new File(parent).mkdirs()
        A: if (exists()) // c:\base doesn't exist
        A: if (mkdir()) // creates c:\base
        A: return true;
        B: if (exists()) // c:\base does exist
        B: return false;
        A: mkdir() // creates c:\base\a and returns true
        B: // parent.mkdirs returned false so doesn't create
        c:\base\b. Returns false.

        I can see no reasonable excuse for thread B's mkdirs to
        fail just because someone else happened to create one of
        the directories in the desired path.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Compile and run the sample code
        2. Check whether both directories were created

        It's timing-dependant so it might not happen every time.



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        Two directories, c:\base\a and c:\base\b, should be created.
        Instead, just one of them gets created.


        REPRODUCIBILITY :
        This bug can be reproduced often.

        ---------- BEGIN SOURCE ----------
        import java.io.File;

        public class MkdirsBug {
           public static void main(String[] args)
           {
              Thread A = new Thread() {
                 public void run()
                 {
                    new File("c:\\base\\a").mkdirs();
                 }
              };
              Thread B = new Thread() {
                 public void run()
                 {
                    new File("c:\\base\\b").mkdirs();
                 }
              };

              A.start();
              B.start();
           }
        }


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

        CUSTOMER WORKAROUND :
        Write your own mkdirs.
        (Review ID: 164083)
        ======================================================================

              sherman Xueming Shen
              nthompsosunw Nathanael Thompson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: