Skip to content

Commit

Permalink
Prevent race condition with concurrent cleanup operation in RobustSha…
Browse files Browse the repository at this point in the history
…redLock

Signed-off-by: Matthias Schneider <[email protected]>
  • Loading branch information
ma30002000 committed Oct 14, 2024
1 parent d4bbcdb commit 109deba
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/cpp/utils/shared_memory/RobustSharedLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,40 +237,53 @@ class RobustSharedLock
bool* was_lock_created,
bool* was_lock_released)
{
auto fd = open(file_path.c_str(), O_RDONLY, 0);
int fd = -1;
do {
fd = open(file_path.c_str(), O_RDONLY, 0);

if (fd != -1)
{
*was_lock_created = false;
}
else
{
*was_lock_created = true;
fd = open(file_path.c_str(), O_CREAT | O_RDONLY, 0666);
}

if (was_lock_released != nullptr)
{
// Lock exclusive
if (0 == flock(fd, LOCK_EX | LOCK_NB))
if (fd != -1)
{
// Exclusive => shared
flock(fd, LOCK_SH | LOCK_NB);
*was_lock_released = true;
return fd;
*was_lock_created = false;
}
else
{
*was_lock_released = false;
*was_lock_created = true;
fd = open(file_path.c_str(), O_CREAT | O_RDONLY, 0666);
}
}

// Lock shared
if (0 != flock(fd, LOCK_SH | LOCK_NB))
{
close(fd);
throw std::runtime_error(("failed to lock " + file_path).c_str());
}
if (was_lock_released != nullptr)
{
// Lock exclusive
if (0 == flock(fd, LOCK_EX | LOCK_NB))
{
// Check if file was deleted by clean up script between open and lock
// if yes, repeat file creation
struct stat buffer = {};
if (stat(file_path.c_str(), &buffer) != 0 && errno == ENOENT)
{
close(fd);
fd = -1;
continue;
}

// Exclusive => shared
flock(fd, LOCK_SH | LOCK_NB);
*was_lock_released = true;
return fd;
}
else
{
*was_lock_released = false;
}
}

// Lock shared
if (0 != flock(fd, LOCK_SH | LOCK_NB))
{
close(fd);
throw std::runtime_error(("failed to lock " + file_path).c_str());
}
} while (fd == -1);

return fd;
}
Expand Down

0 comments on commit 109deba

Please sign in to comment.