Subject: | race condition issue in File::Path::Tiny + patch |
Date: | Sat, 21 Sep 2013 15:20:10 +0400 |
To: | bug-file-path-tiny [...] rt.cpan.org |
From: | Bolshakova Elena <e.a.bolshakova [...] yandex.ru> |
Hi!
Here is my observation about File::Path::Tiny: if multiple processes are creating directories with same names, race condition is possible.
E.g.:
-d $progressive at line 16 returns false (the directory doesn't exist yet)
another process creates the directory (the directory appears)
mkdir( $progressive, $mask ) at line 17 returns false (the directory already exists) -- this is incorrect
Easy demonstration:
perl -MFile::Path::Tiny -e 'for my $i (1 .. 1000) { $path = "/tmp/file_path_tiny_test/dir_".int(rand(100000)); if ( fork() ) {File::Path::Tiny::mk($path) || print "fail\n"; wait();} else { File::Path::Tiny::mk($path) || print "fail\n"; exit;} }' |grep fail |wc -l
Two processes are creating the same directories in parallel, printing 'fail' if mk returns false. These 'fail' strings are signs of collision.
I usually get 20-50 collisions per 1000 iteration.
I suggest additional checking of target directory existance *after* the mkdir before returning false.
The same problem and suggestion applies to removing directories.
I attach my patch and hope you'll accept it.
--
Elena Bolshakova
helena@cpan.org
Message body is not shown because sender requested not to inline it.