Skip to content

Commit ead6035

Browse files
authored
Merge pull request #133 from theia-ide/mp/win32-race
win: fix race condition when starting a watcher
2 parents a76415a + bf3cbaf commit ead6035

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

includes/win32/Watcher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Watcher
3636

3737
std::atomic<bool> mRunning;
3838
SingleshotSemaphore mHasStartedSemaphore;
39+
SingleshotSemaphore mIsRunningSemaphore;
3940
mutable std::mutex mErrorMutex;
4041
std::string mError;
4142

src/win32/Watcher.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::string Watcher::getUTF8Directory(std::wstring path) {
3838
// before returning it to the user
3939
stripNTPrefix(uft16DirectoryString);
4040
}
41-
41+
4242
int utf8length = WideCharToMultiByte(
4343
CP_UTF8,
4444
0,
@@ -239,6 +239,7 @@ void Watcher::start() {
239239
mRunner = std::thread([this] {
240240
// mRunning is set to false in the d'tor
241241
mRunning = true;
242+
mIsRunningSemaphore.signal();
242243
run();
243244
});
244245

@@ -247,11 +248,16 @@ void Watcher::start() {
247248
return;
248249
}
249250

251+
if (!mIsRunningSemaphore.waitFor(std::chrono::seconds(10))) {
252+
setError("Watcher is not started");
253+
return;
254+
}
255+
250256
QueueUserAPC([](__in ULONG_PTR self) {
251257
auto watcher = reinterpret_cast<Watcher*>(self);
252258
watcher->pollDirectoryChanges();
253259
watcher->mHasStartedSemaphore.signal();
254-
} , mRunner.native_handle(), (ULONG_PTR)this);
260+
}, mRunner.native_handle(), (ULONG_PTR)this);
255261

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

0 commit comments

Comments
 (0)