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

Files.createDirectories() can fail if the thread is interrupted

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      The implementation of Files.createDirectories() calls createAndCheckIsDirectory() which in turn uses isDirectory. But isDirectory swallows exceptions, incorrectly returning false if there's a problem. The JavaDocs do document this rather unfortunate behavior and recommend to use a different approach if exceptions need to be distinguished, but the JDK itself doesn't follow that advice.

      It results in the following problem: if you interrupt a thread that's trying to create directories, then you can get misleading FileAlreadyExistsExceptions that aren't actually true. Instead what happened is, InterruptedException got swallowed inside isDirectory and converted into "false" which then sends it down the wrong codepath.

      The isDirectory call is quite unfortunate for this reason. It's not just in this method. Interrupting a thread that tests directory status can potentially send the thread down a totally wrong codepath that could even end up with some sort of state corruption.

      It might therefore be worth considering catching and rethrowing InterruptedException as an unchecked exception, or considering a spec change, or doing something else to ensure this sharp edge doesn't catch others. Unfortunately, because InterruptedException is checked, it can't be so easily added to the method prototype. Maybe a larger project would be to migrate InterruptedException to unchecked, which would clean up a lot of similar bugs related to exception swallowing.


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: