MB-16500 [BP]: Address possible data race in checkpoint remover 67/56067/4
authorabhinavdangeti <abhinav@couchbase.com>
Thu, 8 Oct 2015 22:14:25 +0000 (15:14 -0700)
committerChiyoung Seo <chiyoung@couchbase.com>
Tue, 13 Oct 2015 23:11:11 +0000 (23:11 +0000)
WARNING: ThreadSanitizer: data race (pid=102986)

  Read of size 1 at 0x7d180000c298 by thread T17:
    #0 ClosedUnrefCheckpointRemoverTask::run() /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/checkpoint_remover.cc:139 (ep.so+0x00000003d4a6)
    #1 ExecutorThread::run() /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/executorthread.cc:115 (ep.so+0x0000000f9c4c)
    #2 launch_executor_thread(void*) /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/executorthread.cc:33 (ep.so+0x0000000f9815)
    #3 platform_thread_wrap /home/couchbase/.ccache/tmp/cb_pthread.tmp.9515862b2292.83537.i:0 (libplatform.so.0.1.0+0x0000000041b1)

  Previous write of size 1 at 0x7d180000c298 by thread T18:
    #0 CheckpointVisitor::complete() /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/checkpoint_remover.cc:69 (ep.so+0x00000003e0a1)
    #1 VBCBAdaptor::run() /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/ep.cc:3789 (ep.so+0x00000009d64a)
    #2 ExecutorThread::run() /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/executorthread.cc:115 (ep.so+0x0000000f9c4c)
    #3 launch_executor_thread(void*) /home/couchbase/jenkins/workspace/ep-engine-threadsanitizer-master/ep-engine/src/executorthread.cc:33 (ep.so+0x0000000f9815)
    #4 platform_thread_wrap /home/couchbase/.ccache/tmp/cb_pthread.tmp.9515862b2292.83537.i:0 (libplatform.so.0.1.0+0x0000000041b1)

Change-Id: I8579d5af259490e41028f57f302e547b0826fa61
Reviewed-on: http://review.couchbase.org/55937
Reviewed-by: Chiyoung Seo <chiyoung@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
Reviewed-on: http://review.couchbase.org/56067

src/checkpoint_remover.cc
src/checkpoint_remover.h

index a200da6..c5d0c5b 100644 (file)
@@ -32,7 +32,8 @@ public:
     /**
      * Construct a CheckpointVisitor.
      */
-    CheckpointVisitor(EventuallyPersistentStore *s, EPStats &st, bool *sfin)
+    CheckpointVisitor(EventuallyPersistentStore *s, EPStats &st,
+                      AtomicValue<bool> &sfin)
         : store(s), stats(st), removed(0),
           stateFinalizer(sfin) {}
 
@@ -65,24 +66,23 @@ public:
     }
 
     void complete() {
-        if (stateFinalizer) {
-            *stateFinalizer = true;
-        }
+        bool inverse = false;
+        stateFinalizer.compare_exchange_strong(inverse, true);
     }
 
 private:
     EventuallyPersistentStore *store;
     EPStats                   &stats;
     size_t                     removed;
-    bool                      *stateFinalizer;
+    AtomicValue<bool>         &stateFinalizer;
 };
 
 bool ClosedUnrefCheckpointRemoverTask::run(void) {
-    if (available) {
-        available = false;
+    bool inverse = true;
+    if (available.compare_exchange_strong(inverse, false)) {
         EventuallyPersistentStore *store = engine->getEpStore();
         shared_ptr<CheckpointVisitor> pv(new CheckpointVisitor(store, stats,
-                    &available));
+                                                               available));
         store->visit(pv, "Checkpoint Remover", NONIO_TASK_IDX,
                      Priority::CheckpointRemoverPriority);
     }
index 9958cdc..2a4b939 100644 (file)
@@ -57,7 +57,7 @@ private:
     EventuallyPersistentEngine *engine;
     EPStats                   &stats;
     size_t                     sleepTime;
-    bool                       available;
+    AtomicValue<bool>          available;
 };
 
 #endif  // SRC_CHECKPOINT_REMOVER_H_