Skip to content

Commit

Permalink
Protecting timer with a mutex
Browse files Browse the repository at this point in the history
WARNING: ThreadSanitizer: data race (pid=28641)
  Read of size 1 at 0x7b1000012ad0 by thread T11:
    #8 opflexagent::TunnelEpManager::on_timer(boost::system::error_code const&) lib/TunnelEpManager.cpp:172 (libopflex_agent.so.0+0x243cff)

  Previous write of size 1 at 0x7b1000012ad0 by main thread (mutexes: write M180):
    #4 opflexagent::TunnelEpManager::stop() lib/TunnelEpManager.cpp:88 (libopflex_agent.so.0+0x24393e)
    #5 opflexagent::OVSRenderer::stop() ovs/OVSRenderer.cpp:285 (libopflex_agent_renderer_openvswitch.so+0xcb84b)
    #6 opflexagent::Agent::stop() lib/Agent.cpp:718 (libopflex_agent.so.0+0x2723d8)
    #7 AgentLauncher::run() cmd/opflex_agent.cpp:128 (opflex_agent+0x3a4a7)
    #8 main cmd/opflex_agent.cpp:322 (opflex_agent+0x16dc2)

  Location is heap block of size 64 at 0x7b1000012ac0 allocated by main thread:
    #0 operator new(unsigned long) <null> (libtsan.so.0+0x77cf2)
    #1 opflexagent::TunnelEpManager::start() lib/TunnelEpManager.cpp:75 (libopflex_agent.so.0+0x247908)
    #2 opflexagent::OVSRenderer::start() ovs/OVSRenderer.cpp:119 (libopflex_agent_renderer_openvswitch.so+0xd55cc)
    #3 opflexagent::Agent::start() lib/Agent.cpp:601 (libopflex_agent.so.0+0x2783ea)
    #4 AgentLauncher::run() cmd/opflex_agent.cpp:120 (opflex_agent+0x3a438)
    #5 main cmd/opflex_agent.cpp:322 (opflex_agent+0x16dc2)

  Mutex M180 (0x7fff82ef6dc8) created at:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x41d5b)
    #1 __gthread_mutex_lock /usr/include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:749 (opflex_agent+0x3a3f0)
    #2 std::mutex::lock() /usr/include/c++/9/bits/std_mutex.h:100 (opflex_agent+0x3a3f0)
    #3 std::unique_lock<std::mutex>::lock() /usr/include/c++/9/bits/unique_lock.h:141 (opflex_agent+0x3a3f0)
    #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&) /usr/include/c++/9/bits/unique_lock.h:71 (opflex_agent+0x3a3f0)
    #5 AgentLauncher::run() cmd/opflex_agent.cpp:115 (opflex_agent+0x3a3f0)
    #6 main cmd/opflex_agent.cpp:322 (opflex_agent+0x16dc2)

  Thread T11 (tid=28681, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x2d3be)
    #1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xd0b04)
    #2 AgentLauncher::run() cmd/opflex_agent.cpp:120 (opflex_agent+0x3a438)
    #3 main cmd/opflex_agent.cpp:322 (opflex_agent+0x16dc2)

Signed-off-by: Gautam Venkataramanan <gautam.chennai@gmail.com>
  • Loading branch information
gautvenk committed Sep 3, 2020
1 parent a167d4a commit 3d3c632
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions agent-ovs/lib/TunnelEpManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void TunnelEpManager::start() {
mutator.commit();

#ifdef HAVE_IFADDRS_H
const std::lock_guard<std::mutex> guard(timer_mutex);
timer.reset(new deadline_timer(agent_io, milliseconds(0)));
timer->async_wait(bind(&TunnelEpManager::on_timer, this, error));
#else
Expand All @@ -84,6 +85,7 @@ void TunnelEpManager::stop() {
LOG(DEBUG) << "Stopping tunnel endpoint manager";

stopping = true;
const std::lock_guard<std::mutex> guard(timer_mutex);
if (timer) {
timer->cancel();
}
Expand Down Expand Up @@ -169,6 +171,7 @@ static string getInterfaceAddressV4(const string& iface) {
void TunnelEpManager::on_timer(const error_code& ec) {
if (ec) {
// shut down the timer when we get a cancellation
const std::lock_guard<std::mutex> guard(timer_mutex);
timer.reset();
return;
}
Expand Down Expand Up @@ -305,6 +308,7 @@ void TunnelEpManager::on_timer(const error_code& ec) {
}

if (!stopping) {
const std::lock_guard<std::mutex> guard(timer_mutex);
timer->expires_at(timer->expires_at() + milliseconds(timer_interval));
timer->async_wait(bind(&TunnelEpManager::on_timer, this, error));
}
Expand Down
1 change: 1 addition & 0 deletions agent-ovs/lib/include/opflexagent/TunnelEpManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class TunnelEpManager : private boost::noncopyable {
boost::asio::io_service& agent_io;
long timer_interval;
std::unique_ptr<boost::asio::deadline_timer> timer;
std::mutex timer_mutex;

void on_timer(const boost::system::error_code& ec);

Expand Down

0 comments on commit 3d3c632

Please sign in to comment.