Skip to content

Commit

Permalink
util: Make LockDirectory stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Feb 13, 2025
1 parent 989d8ac commit 01c6c70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,13 +1244,13 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::Success);

// Another lock on the directory from the same thread should succeed
BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::Success);
BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::ErrorLock);

// Another lock on the directory from a different thread within the same process should succeed
util::LockResult threadresult;
std::thread thr([&] { threadresult = util::LockDirectory(dirname, lockname); });
thr.join();
BOOST_CHECK_EQUAL(threadresult, util::LockResult::Success);
BOOST_CHECK_EQUAL(threadresult, util::LockResult::ErrorLock);
#ifndef WIN32
// Try to acquire lock in child process while we're holding it, this should fail.
BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
Expand Down
3 changes: 2 additions & 1 deletion src/util/fs_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_nam

// If a lock for this directory already exists in the map, don't try to re-lock it
if (dir_locks.count(fs::PathToString(pathLockFile))) {
return LockResult::Success;
LogError("Error while attempting to lock directory %s: Lock already taken", fs::PathToString(directory));
return LockResult::ErrorLock;
}

// Create empty lock file if it doesn't exist.
Expand Down

0 comments on commit 01c6c70

Please sign in to comment.