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

[15341] Protect access to reader listeners #2898

Merged
merged 4 commits into from
Aug 3, 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
13 changes: 10 additions & 3 deletions src/cpp/fastdds/subscriber/DataReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,12 +1227,14 @@ bool DataReaderImpl::lifespan_expired()
ReturnCode_t DataReaderImpl::set_listener(
DataReaderListener* listener)
{
std::lock_guard<std::mutex> _(listener_mutex_);
listener_ = listener;
return ReturnCode_t::RETCODE_OK;
}

const DataReaderListener* DataReaderImpl::get_listener() const
{
std::lock_guard<std::mutex> _(listener_mutex_);
return listener_;
}

Expand Down Expand Up @@ -1637,11 +1639,16 @@ fastrtps::TopicAttributes DataReaderImpl::topic_attributes() const
DataReaderListener* DataReaderImpl::get_listener_for(
const StatusMask& status)
{
if (listener_ != nullptr &&
user_datareader_->get_status_mask().is_active(status))
{
return listener_;
std::lock_guard<std::mutex> _(listener_mutex_);

if (listener_ != nullptr &&
user_datareader_->get_status_mask().is_active(status))
{
return listener_;
}
}

return subscriber_->get_listener_for(status);
}

Expand Down
1 change: 1 addition & 0 deletions src/cpp/fastdds/subscriber/DataReaderImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class DataReaderImpl

//!Listener
DataReaderListener* listener_ = nullptr;
mutable std::mutex listener_mutex_;

fastrtps::rtps::GUID_t guid_;

Expand Down
14 changes: 8 additions & 6 deletions src/cpp/rtps/reader/StatefulReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ bool StatefulReader::matched_writer_add(
const WriterProxyData& wdata)
{
assert(wdata.guid() != c_Guid_Unknown);
ReaderListener* listener = nullptr;

{
std::unique_lock<RecursiveTimedMutex> guard(mp_mutex);
Expand All @@ -193,6 +194,7 @@ bool StatefulReader::matched_writer_add(
return false;
}

listener = mp_listener;
bool is_same_process = RTPSDomainImpl::should_intraprocess_between(m_guid, wdata.guid());
bool is_datasharing = !is_same_process && is_datasharing_compatible_with(wdata);

Expand All @@ -210,12 +212,11 @@ bool StatefulReader::matched_writer_add(
}
}

if (nullptr != mp_listener)
if (nullptr != listener)
{
// call the listener without the lock taken
guard.unlock();
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::CHANGED_QOS_WRITER, wdata.guid(),
&wdata);
listener->on_writer_discovery(this, WriterDiscoveryInfo::CHANGED_QOS_WRITER, wdata.guid(), &wdata);
}
return false;
}
Expand Down Expand Up @@ -318,9 +319,9 @@ bool StatefulReader::matched_writer_add(
}
}

if (nullptr != mp_listener)
if (nullptr != listener)
{
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::DISCOVERED_WRITER, wdata.guid(), &wdata);
listener->on_writer_discovery(this, WriterDiscoveryInfo::DISCOVERED_WRITER, wdata.guid(), &wdata);
}

return true;
Expand Down Expand Up @@ -385,8 +386,9 @@ bool StatefulReader::matched_writer_remove(
if (nullptr != mp_listener)
{
// call the listener without the lock taken
ReaderListener* listener = mp_listener;
lock.unlock();
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::REMOVED_WRITER, writer_guid, nullptr);
listener->on_writer_discovery(this, WriterDiscoveryInfo::REMOVED_WRITER, writer_guid, nullptr);
}
}
else
Expand Down
15 changes: 10 additions & 5 deletions src/cpp/rtps/reader/StatelessReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,22 @@ StatelessReader::StatelessReader(
bool StatelessReader::matched_writer_add(
const WriterProxyData& wdata)
{
ReaderListener* listener = nullptr;

{
std::unique_lock<RecursiveTimedMutex> guard(mp_mutex);
listener = mp_listener;

for (const RemoteWriterInfo_t& writer : matched_writers_)
{
if (writer.guid == wdata.guid())
{
logWarning(RTPS_READER, "Attempting to add existing writer");
if (nullptr != mp_listener)
if (nullptr != listener)
{
// call the listener without the lock taken
guard.unlock();
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::CHANGED_QOS_WRITER, wdata.guid(),
listener->on_writer_discovery(this, WriterDiscoveryInfo::CHANGED_QOS_WRITER, wdata.guid(),
&wdata);
}
return false;
Expand Down Expand Up @@ -177,9 +181,9 @@ bool StatelessReader::matched_writer_add(
}
}

if (nullptr != mp_listener)
if (nullptr != listener)
{
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::DISCOVERED_WRITER, wdata.guid(), &wdata);
listener->on_writer_discovery(this, WriterDiscoveryInfo::DISCOVERED_WRITER, wdata.guid(), &wdata);
}

return true;
Expand Down Expand Up @@ -229,8 +233,9 @@ bool StatelessReader::matched_writer_remove(
if (nullptr != mp_listener)
{
// call the listener without lock
ReaderListener* listener = mp_listener;
guard.unlock();
mp_listener->on_writer_discovery(this, WriterDiscoveryInfo::REMOVED_WRITER, writer_guid, nullptr);
listener->on_writer_discovery(this, WriterDiscoveryInfo::REMOVED_WRITER, writer_guid, nullptr);
}
return true;
}
Expand Down