LOG4J2-3452: Fix race condition in FileUtils.mkdir()#809
LOG4J2-3452: Fix race condition in FileUtils.mkdir()#809ppkarwasz merged 2 commits intoapache:release-2.xfrom
Conversation
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.
|
@stefanvodita, |
|
if the directory exists, it still tries to create it - which breaks when using a policy file - you should have left "if exists just return" |
|
Just to check that I understand the problem - is there an exception you are encountering, which we could have caught when calling |
|
the problem is that althought the logs dir exists, you call to create it and java security manager kicks in and it requires these two lines (i only had the 2nd initially, because in my view log4j should not attempt to write the dir): config - using log4j 1-2 bridge: anyhow: the java security manager / policy files are not often used and are apparently on the way out, no need to fix - hopefully the one who has the same issue finds this comment :-) |
|
The main point of @stefanvodita's PR is that I don't see anything wrong in adding a Remark: with |
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.
Note: I didn't manage to run the tests with Maven, but I ran the tests in
FileUtilsTest from IntelliJ. I'll come back to this when I have more time.