MB-19257: Fix data race on ExecutorThread::now 75/62975/6
authorDave Rigby <daver@couchbase.com>
Thu, 8 Oct 2015 15:49:52 +0000 (15:49 +0000)
committerChiyoung Seo <chiyoung@couchbase.com>
Sat, 23 Apr 2016 01:08:32 +0000 (01:08 +0000)
This value is accessed without a lock from addWorkerStats. Change to
atomic to fix race.

As reported by ThreadSanitizer:

WARNING: ThreadSanitizer: data race (pid=18761)
  Read of size 8 at 0x7d4400007fa8 by main thread (mutexes: write M19371):
    #0 ExecutorThread::getCurTime() /home/couchbase/server/ep-engine/src/executorthread.h:129:46 (ep.so+0x0000000e67a0)
    #1 addWorkerStats(char const*, ExecutorThread*, void const*, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/ep-engine/src/executorpool.cc:748 (ep.so+0x0000000e67a0)
    #2 ExecutorPool::doWorkerStat(EventuallyPersistentEngine*, void const*, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/ep-engine/src/executorpool.cc:760 (ep.so+0x0000000e67a0)
    #3 EventuallyPersistentEngine::doDispatcherStats(void const*, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/ep-engine/src/ep_engine.cc:4352:5 (ep.so+0x0000000bc72d)
    #4 EventuallyPersistentEngine::getStats(void const*, char const*, int, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/ep-engine/src/ep_engine.cc:4588 (ep.so+0x0000000bc72d)
    #5 EvpGetStats(engine_interface*, void const*, char const*, int, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/ep-engine/src/ep_engine.cc:214:38 (ep.so+0x0000000ab70e)
    #6 mock_get_stats(engine_interface*, void const*, char const*, int, void (*)(char const*, unsigned short, char const*, unsigned int, void const*)) /home/couchbase/server/memcached/programs/engine_testapp/engine_testapp.cc:239:19 (engine_testapp+0x0000004c553d)
    #7 test_worker_stats(engine_interface*, engine_interface_v1*) /home/couchbase/server/ep-engine/tests/ep_testsuite.cc:8901:24 (ep_testsuite.so+0x000000039908)
    #8 execute_test(test, char const*, char const*) /home/couchbase/server/memcached/programs/engine_testapp/engine_testapp.cc:1090:19 (engine_testapp+0x0000004c4142)
    #9 main /home/couchbase/server/memcached/programs/engine_testapp/engine_testapp.cc:1439 (engine_testapp+0x0000004c4142)

  Previous write of size 8 at 0x7d4400007fa8 by thread T7 (mutexes: write M12645):
    #0 TaskQueue::_doSleep(ExecutorThread&) /home/couchbase/server/ep-engine/src/taskqueue.cc:78:5 (ep.so+0x00000012eb21)
    #1 TaskQueue::_fetchNextTask(ExecutorThread&, bool) /home/couchbase/server/ep-engine/src/taskqueue.cc:117:21 (ep.so+0x00000012ed66)
    #2 TaskQueue::fetchNextTask(ExecutorThread&, bool) /home/couchbase/server/ep-engine/src/taskqueue.cc:160:17 (ep.so+0x00000012f907)
    #3 ExecutorPool::_nextTask(ExecutorThread&, unsigned char) /home/couchbase/server/ep-engine/src/executorpool.cc:226:17 (ep.so+0x0000000dfa6f)
    #4 ExecutorPool::nextTask(ExecutorThread&, unsigned char) /home/couchbase/server/ep-engine/src/executorpool.cc:241:21 (ep.so+0x0000000dfac6)
    #5 ExecutorThread::run() /home/couchbase/server/ep-engine/src/executorthread.cc:81:28 (ep.so+0x0000000e9cfe)
    #6 launch_executor_thread(void*) /home/couchbase/server/ep-engine/src/executorthread.cc:33:9 (ep.so+0x0000000e9b05)
    #7 platform_thread_wrap /home/couchbase/server/platform/src/cb_pthreads.c:23:5 (libplatform.so.0.1.0+0x000000003dc1)

Change-Id: Idcaea9a157293dbf95ca236354673556a2f3c4ac
Reviewed-on: http://review.couchbase.org/62975
Well-Formed: buildbot <build@couchbase.com>
Reviewed-by: Jim Walker <jim@couchbase.com>
Reviewed-by: Will Gardner <will.gardner@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
src/executorthread.h

index b13dd77..2c42862 100644 (file)
@@ -129,7 +129,7 @@ private:
     const std::string name;
     AtomicValue<executor_state_t> state;
 
-    hrtime_t     now;  // record of current time
+    AtomicValue<hrtime_t> now;  // record of current time
     hrtime_t waketime; // set to the earliest
 
     hrtime_t taskStart;