Skip to content

Commit

Permalink
Fix race in memcached_uptime access
Browse files Browse the repository at this point in the history
Identified by ThreadSanitizer:

WARNING: ThreadSanitizer: data race (pid=34137)
  Write of size 4 at 0x7fa0ab35b288 by main thread:
    #0 mc_time_clock_tick() /home/daver/repos/couchbase/server/memcached/daemon/mc_time.cc:171 (memcached+0x0000000f9df3)
    #1 mc_time_clock_event_handler(int, short, void*) /home/daver/repos/couchbase/server/memcached/daemon/mc_time.cc:158 (memcached+0x0000000fa08f)
    #2 event_process_active_single_queue /home/couchbase/jenkins/workspace/cbdeps-build/label/ubuntu14.04/release/sherlock/deps/packages/build/libevent/libevent-prefix/src/libevent/event.c:1368 (libevent_core-2.0.so.5+0x00000000b7cb)
    #3 __libc_start_main /build/buildd/eglibc-2.19/csu/libc-start.c:287 (libc.so.6+0x000000021ec4)

  Previous read of size 4 at 0x7fa0ab35b288 by thread T12 (mutexes: write M45839, write M45891):
    #0 mc_time_get_current_time /home/daver/repos/couchbase/server/memcached/daemon/mc_time.cc:94 (memcached+0x0000000fa0b6)
    #1 do_item_update /home/daver/repos/couchbase/server/memcached/engines/default_engine/items.c:358 (default_engine.so+0x000000008109)
    #2 item_get /home/daver/repos/couchbase/server/memcached/engines/default_engine/items.c:821 (default_engine.so+0x000000007deb)
    #3 default_get /home/daver/repos/couchbase/server/memcached/engines/default_engine/default_engine.c:335 (default_engine.so+0x000000003ef5)
    #4 EWB_Engine::get(engine_interface*, void const*, void**, void const*, int, unsigned short) /home/daver/repos/couchbase/server/memcached/engines/ewouldblock_engine/ewouldblock_engine.cc:247 (ewouldblock_engine.so+0x000000004fc1)
    #5 bucket_get /home/daver/repos/couchbase/server/memcached/daemon/memcached.cc:123 (memcached+0x0000000e096a)
    #6 process_bin_packet(conn*) /home/daver/repos/couchbase/server/memcached/daemon/memcached.cc:5051 (memcached+0x0000000d2100)
    memcached#7 run_event_loop /home/daver/repos/couchbase/server/memcached/daemon/connections.cc:174 (memcached+0x0000000c70c3)
    memcached#8 thread_libevent_process(int, short, void*) /home/daver/repos/couchbase/server/memcached/daemon/thread.cc:374 (memcached+0x0000000f8fc0)
    memcached#9 event_persist_closure /home/couchbase/jenkins/workspace/cbdeps-build/label/ubuntu14.04/release/sherlock/deps/packages/build/libevent/libevent-prefix/src/libevent/event.c:1319 (libevent_core-2.0.so.5+0x00000000b6c7)
    memcached#10 platform_thread_wrap /home/daver/repos/couchbase/server/platform/src/cb_pthreads.c:23 (libplatform.so.0.1.0+0x000000003730)

Change-Id: I9d73a6f5553d88beb1bcc2526763d892fe1b990f
Reviewed-on: http://review.couchbase.org/52484
Tested-by: buildbot <[email protected]>
Reviewed-by: Trond Norbye <[email protected]>
  • Loading branch information
daverigby authored and trondn committed Jun 30, 2015
1 parent f500a26 commit 8a92da8
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions daemon/mc_time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "memcached.h"
#include "mc_time.h"

#include <atomic>

extern volatile sig_atomic_t memcached_shutdown;

/*
Expand All @@ -48,7 +50,7 @@ const time_t memcached_check_system_time = 60;
*/
const time_t memcached_maximum_relative_time = 60*60*24*30;

static volatile rel_time_t memcached_uptime = 0;
static std::atomic<rel_time_t> memcached_uptime(0);
static volatile time_t memcached_epoch = 0;
static volatile uint64_t memcached_monotonic_start = 0;
static struct event_base* main_ev_base = NULL;
Expand Down Expand Up @@ -189,7 +191,7 @@ static void mc_time_clock_tick(void) {
"memcached_uptime = %u, new memcached_epoch = %lu, "
"next check %lu\n",
difference, memcached_epoch,
memcached_uptime, (timeofday.tv_sec - memcached_uptime),
memcached_uptime.load(), (timeofday.tv_sec - memcached_uptime),
check_system_time + memcached_check_system_time);
}
/* adjust memcached_epoch to ensure correct timeofday can
Expand Down

0 comments on commit 8a92da8

Please sign in to comment.