aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/native/fsevents.cc
diff options
context:
space:
mode:
authorGravatar Julio Merino <jmmv@google.com>2017-03-07 14:57:33 +0000
committerGravatar Vladimir Moskva <vladmos@google.com>2017-03-07 15:26:40 +0000
commit51eafed7eba88ae716da4f33f2133d4860862438 (patch)
treee5f7ac0da39234bcfe84371196dc89dffb3b43de /src/main/native/fsevents.cc
parent94d8f4e9c8c8757aa7ab7c1a7c3e9afb34039127 (diff)
Initialize the mutex in JNIEventsDiffAwareness
pthread mutexes must be initialized with pthread_mutex_init and cleaned up with pthread_mutex_destroy. This seems to fix a race where poll() would access invalid array indexes on an array constructed based on the size of a shared list protected by the mutex. This is understandable because the mutex may not have been doing anything due to the lack of its proper initialization -- and, if so, I'm surprised the consequences were not more catastrophic. As with any race condition, it is hard to confirm that this fixes the observed problem, but I could trivially reproduce this issue earlier and now I cannot with this fix after tens of runs. See reproduction code in the referenced bug for details on how to expose the issue. Fixes #1676. -- Change-Id: Ia5a4a8f12da7c3780f33266b9922eeba7645b3a4 Reviewed-on: https://cr.bazel.build/9230 PiperOrigin-RevId: 149414125 MOS_MIGRATED_REVID=149414125
Diffstat (limited to 'src/main/native/fsevents.cc')
-rw-r--r--src/main/native/fsevents.cc6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/main/native/fsevents.cc b/src/main/native/fsevents.cc
index b051580ee7..22f9d98152 100644
--- a/src/main/native/fsevents.cc
+++ b/src/main/native/fsevents.cc
@@ -32,6 +32,10 @@ struct JNIEventsDiffAwareness {
// The former is called inside the FsEvents run loop and the latter
// from Java threads.
pthread_mutex_t mutex;
+
+ JNIEventsDiffAwareness() { pthread_mutex_init(&mutex, nullptr); }
+
+ ~JNIEventsDiffAwareness() { pthread_mutex_destroy(&mutex); }
};
// Callback called when an event is reported by the FSEvents API
@@ -64,7 +68,7 @@ Java_com_google_devtools_build_lib_skyframe_MacOSXFsEventsDiffAwareness_create(
JNIEnv *env, jobject fsEventsDiffAwareness, jobjectArray paths,
jdouble latency) {
// Create a FSEventStreamContext to pass around (env, fsEventsDiffAwareness)
- JNIEventsDiffAwareness *info = new JNIEventsDiffAwareness;
+ JNIEventsDiffAwareness *info = new JNIEventsDiffAwareness();
FSEventStreamContext context;
context.version = 0;