[ 
https://issues.apache.org/jira/browse/LOG4J2-3452?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17514954#comment-17514954
 ] 

ASF subversion and git services commented on LOG4J2-3452:
---------------------------------------------------------

Commit 396ec807aa277047c5119ce60fcc14bee7b9ffcc in logging-log4j2's branch 
refs/heads/release-2.x from Stefan Vodita
[ https://gitbox.apache.org/repos/asf?p=logging-log4j2.git;h=396ec80 ]

LOG4J2-3452: Fix race condition in FileUtils.mkdir() (#809)

* LOG4J2-3452: Fix race condition in FileUtils.mkdir()

A race condition could appear when 2 threads were trying to create the
same directory. Both would check that the directory did not exist, then
one would create the directory and the other would throw an exception.

This was reproduced with a unit test included this commit and fixed by
using Files.createDirectories() instead of File.mkdirs(), so that the
existence check and directory creation are done as an atomic operation.


> Race condition in FileUtils.mkdir()
> -----------------------------------
>
>                 Key: LOG4J2-3452
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3452
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.13.0
>            Reporter: Stefan Vodita
>            Priority: Major
>
> 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.
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to