avoid race conditions in createDirectoryIfMissing (#2808)