Skip to content

Commit

Permalink
fio: fix race condition in mktree
Browse files Browse the repository at this point in the history
Despite the lack of documentation, fio.mktree() was designed to work
similar to mkdir -p: it creates the directory along with it's parents
and doesn't complain about existing ones.

But this function was subject to a race if two different processes were
trying to create the same directory at the same time. It was caused by
the fact that directory existence check and its creation aren't atomic.

This patch fixes the race by impoving error handling: it's not an error
if directory exists, even if it was created by someone else and mktree
failed.

Related to tarantool/doc#1063
Closes tarantool#4660
  • Loading branch information
HustonMmmavr authored and avtikhon committed Feb 3, 2020
1 parent c69a4f9 commit ffd8ad2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lua/fio.lua
Expand Up @@ -364,7 +364,11 @@ fio.mktree = function(path, mode)
local stat = fio.stat(current_dir)
if stat == nil then
local st, err = fio.mkdir(current_dir, mode)
if err ~= nil then
-- fio.stat() and fio.mkdir() above are separate calls
-- and a file system may be changed between them. So
-- if the error here is due to an existing directory,
-- the function should not report an error.
if err ~= nil and not fio.path.is_dir(current_dir) then
return false, string.format("Error creating directory %s: %s",
current_dir, tostring(err))
end
Expand Down

0 comments on commit ffd8ad2

Please sign in to comment.