Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: liveliness core dump caused by omit mutex lock #2828

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ if(MSVC OR MSVC_IDE)
# C4555 expression has no effect; expected expression with side-effect
# C4715: 'Test': not all control paths return a value
# C5038 data member 'member1' will be initialized after data member 'member2'
add_compile_options(/w34101 /w34189 /w34555 /w34715 /w35038)
# C4100 'identifier' : unreferenced formal parameter (matches clang -Wunused-lambda-capture)
add_compile_options(/w34101 /w34189 /w34555 /w34715 /w35038 /w44100)

if(EPROSIMA_BUILD)
string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
Expand Down
29 changes: 13 additions & 16 deletions src/cpp/rtps/writer/LivelinessManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LivelinessManager::LivelinessManager(

LivelinessManager::~LivelinessManager()
{
std::unique_lock<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> _(mutex_);
timer_owner_ = nullptr;
timer_.cancel_timer();
}
Expand All @@ -52,12 +52,11 @@ bool LivelinessManager::add_writer(
{
// collection guard
std::lock_guard<shared_mutex> _(col_mutex_);
// writers_ elements guard
std::lock_guard<std::mutex> __(mutex_);

for (LivelinessData& writer : writers_)
{
// writers_ elements guard
std::lock_guard<std::mutex> __(mutex_);

if (writer.guid == guid &&
writer.kind == kind &&
writer.lease_duration == lease_duration)
Expand Down Expand Up @@ -101,11 +100,11 @@ bool LivelinessManager::remove_writer(
{
// collection guard
std::lock_guard<shared_mutex> _(col_mutex_);
// writers_ elements guard
std::lock_guard<std::mutex> __(mutex_);

removed = writers_.remove_if([guid, kind, lease_duration, &status, this](LivelinessData& writer)
removed = writers_.remove_if([guid, kind, lease_duration, &status](LivelinessData& writer)
{
// writers_ elements guard
std::lock_guard<std::mutex> _(mutex_);
status = writer.status;
return writer.guid == guid &&
writer.kind == kind &&
Expand Down Expand Up @@ -288,20 +287,18 @@ bool LivelinessManager::assert_liveliness(

bool LivelinessManager::calculate_next()
{
timer_owner_ = nullptr;
// Keep this lock order to prevent ABBA deadlocks
shared_lock<shared_mutex> _(col_mutex_);
std::lock_guard<std::mutex> __(mutex_);

bool any_alive = false;
steady_clock::time_point min_time = steady_clock::now() + nanoseconds(c_TimeInfinite.to_ns());

bool any_alive = false;
timer_owner_ = nullptr;

// collection guard
shared_lock<shared_mutex> _(col_mutex_);

for (LivelinessData& writer : writers_)
{
// writers_ elements guard
std::lock_guard<std::mutex> __(mutex_);

if (writer.status == LivelinessData::WriterStatus::ALIVE)
{
if (writer.time < min_time)
Expand Down Expand Up @@ -362,12 +359,12 @@ bool LivelinessManager::timer_expired()
bool LivelinessManager::is_any_alive(
LivelinessQosPolicyKind kind)
{
// Keep this lock order to prevent ABBA deadlocks
shared_lock<shared_mutex> _(col_mutex_);
std::lock_guard<std::mutex> __(mutex_);

for (const auto& writer : writers_)
{
std::unique_lock<std::mutex> lock(mutex_);

if (writer.kind == kind && writer.status == LivelinessData::WriterStatus::ALIVE)
{
return true;
Expand Down