Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3452

Race condition in FileUtils.mkdir()

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.13.0
    • None
    • None
    • None

    Description

      I noticed an error like:

      ERROR Unable to create file ...

      at org.apache.logging.log4j.core.util.FileUtils.mkdir(FileUtils.java:118)

       

      The error could be due to a race condition:

      Thread A: dir.exists() -> false

      Thread B: dir.exists() -> false

      Thread B: dir.mkdirs() -> succeeds

      Thread A: dir.mkdirs() -> throws exception

       

      I wrote a unit test which reproduces the error:

      public void testMkdirConcurrent() throws IOException {
              final File file = new File("target/test/test");

              for (int i = 0; i < 1000; i++) {
                  new Thread(() -> assertDoesNotThrow(() -> FileUtils.mkdir(file, true))).start();

              org.apache.commons.io.FileUtils.deleteDirectory(file);
      }

       

      As a fix, we can use Files.createDirectories(), since this does the existence check and creation as one atomic operation.

       

      I will create a PR with the unit test and a fix.

       

       

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              stefanvodita Stefan Vodita
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m