DirectoryWatcher: close a possible window of race on Windows

The initial scan occurring before the watcher is ready allows a race
condition where a change occurs before the initial scan completes.
Ensure that we wait for the watcher to begin executing the initial scan.

Addresses some feedback from Adrian McCarthy in post-commit review.
This commit is contained in:
Saleem Abdulrasool 2021-06-14 19:41:30 -07:00
parent 680d3f8f17
commit a6948da86a
1 changed files with 3 additions and 0 deletions
clang/lib/DirectoryWatcher/windows

View File

@ -133,6 +133,9 @@ DirectoryWatcherWindows::~DirectoryWatcherWindows() {
} }
void DirectoryWatcherWindows::InitialScan() { void DirectoryWatcherWindows::InitialScan() {
std::unique_lock<std::mutex> lock(Mutex);
Ready.wait(lock, [this] { return this->WatcherActive; });
Callback(getAsFileEvents(scanDirectory(Path.data())), /*IsInitial=*/true); Callback(getAsFileEvents(scanDirectory(Path.data())), /*IsInitial=*/true);
} }