Skip to content

Commit

Permalink
Merge pull request #133 from theia-ide/mp/win32-race
Browse files Browse the repository at this point in the history
win: fix race condition when starting a watcher
  • Loading branch information
implausible authored Mar 25, 2021
2 parents a76415a + bf3cbaf commit ead6035
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions includes/win32/Watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Watcher

std::atomic<bool> mRunning;
SingleshotSemaphore mHasStartedSemaphore;
SingleshotSemaphore mIsRunningSemaphore;
mutable std::mutex mErrorMutex;
std::string mError;

Expand Down
10 changes: 8 additions & 2 deletions src/win32/Watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ std::string Watcher::getUTF8Directory(std::wstring path) {
// before returning it to the user
stripNTPrefix(uft16DirectoryString);
}

int utf8length = WideCharToMultiByte(
CP_UTF8,
0,
Expand Down Expand Up @@ -239,6 +239,7 @@ void Watcher::start() {
mRunner = std::thread([this] {
// mRunning is set to false in the d'tor
mRunning = true;
mIsRunningSemaphore.signal();
run();
});

Expand All @@ -247,11 +248,16 @@ void Watcher::start() {
return;
}

if (!mIsRunningSemaphore.waitFor(std::chrono::seconds(10))) {
setError("Watcher is not started");
return;
}

QueueUserAPC([](__in ULONG_PTR self) {
auto watcher = reinterpret_cast<Watcher*>(self);
watcher->pollDirectoryChanges();
watcher->mHasStartedSemaphore.signal();
} , mRunner.native_handle(), (ULONG_PTR)this);
}, mRunner.native_handle(), (ULONG_PTR)this);

if (!mHasStartedSemaphore.waitFor(std::chrono::seconds(10))) {
setError("Watcher is not started");
Expand Down

0 comments on commit ead6035

Please sign in to comment.