In the same way the cache_path function in the WP_Optimize_Minify_Cache_Functions class has a similar reverse race condition where you first check if the directory exists then try to create it, and if someone else beats you to it it will trigger an error.
if (!is_dir($target) && !wp_mkdir_p($target)) {
error_log(‘WP_Optimize_Minify_Cache_Functions::cache_path(): The folder “‘.$target.'” could not be created.’);
}
Could perhaps be:
if (!is_dir($target) && !wp_mkdir_p($target)) {
if(!is_dir($target))
error_log(‘WP_Optimize_Minify_Cache_Functions::cache_path(): The folder “‘.$target.'” could not be created.’);
}
-
This reply was modified 3 years, 7 months ago by
mrsmurf.
@mrsmurf Thanks for sharing this, I’ll share the same with our development team.
@mrsmurf We are applying your first suggestion in our next release.
However, I don’t think the second one has a race condition.
If some other process/user created the director wp_mkdir_p will return true if the directory already exists.
https://developer.wordpress.org/reference/functions/wp_mkdir_p/
N.B. Your suggested code still has a race condition. file_exists() and unlink() run consecutively, and in the mean-while another process might have removed the file – so there’s the same window in between those two as there is currently between readdir() and unlink().
(I have proposed to our developer a superior method that will do what is intended, and that will be implemented instead).