camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bacar <ba...@acar.org.uk>
Subject Re: FileUtil.createNewFile race condition issue
Date Wed, 18 Feb 2015 10:34:41 GMT
Here's a simple test which demonstrates the issue:
https://gist.github.com/bacar/b99e991438a848078712

It spins up 2 threads which attempt File.createNewFile on the same file,
catch IOExceptions, and check for file existence.

After several rounds of the two threads fighting over the file (with
createNewFile returning false if the other thread still has 'ownership'), I
eventually get:

1: failed to create file.
1: creating file
1: failed to create file.
1: creating file
2: deleted file
2: creating file
1: exception! File exists? true
java.io.IOException: Access is denied
	at java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.io.File.createNewFile(File.java:883)
	at
com.barcap.edt.xtrm.zenith.common.core.instrument.CreateFileTest$1.run(CreateFileTest.java:36)
	at java.lang.Thread.run(Thread.java:662)
2: created file.
2: deleting file
2: deleted file

Note that:
- the last thing that thread 1 and thread 2 both did before the exception
was to attempt to create the file.
- at the point the exception occurred in thread 1, the file did exist
- however we can see from the line immediately following the exception that
thread _2_ created that file, ie thread 2 has ownership; thread 1 _cannot_
assume that it owns the file merely because it exists.





--
View this message in context: http://camel.465427.n5.nabble.com/FileUtil-createNewFile-race-condition-issue-tp5762853p5762897.html
Sent from the Camel Development mailing list archive at Nabble.com.

Mime
View raw message