Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cleanup race condition with exclusive and shared lock files #5319

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/cpp/utils/shared_memory/RobustExclusiveLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,41 @@ class RobustExclusiveLock
const std::string& file_path,
bool* was_lock_created)
{
auto 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 (fd == -1)
{
return -1;
}

// Lock the file
if (0 != flock(fd, LOCK_EX | LOCK_NB))
{
close(fd);
return -1;
}

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 (fd == -1)
{
return -1;
}

// Lock the file
if (0 != flock(fd, LOCK_EX | LOCK_NB))
{
close(fd);
return -1;
}

// 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;
}
} while (fd == -1);
return fd;
}

Expand Down
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