From a6948da86ad7e78d66b26263c2681ef6385cc234 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 14 Jun 2021 19:41:30 -0700 Subject: [PATCH] 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. --- .../lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp index 847d20bfbb6f..8a4f5a87d967 100644 --- a/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp +++ b/clang/lib/DirectoryWatcher/windows/DirectoryWatcher-windows.cpp @@ -133,6 +133,9 @@ DirectoryWatcherWindows::~DirectoryWatcherWindows() { } void DirectoryWatcherWindows::InitialScan() { + std::unique_lock lock(Mutex); + Ready.wait(lock, [this] { return this->WatcherActive; }); + Callback(getAsFileEvents(scanDirectory(Path.data())), /*IsInitial=*/true); }