Skip to content

Commit

Permalink
Avoid data race on UDPChannelResource (#2652)
Browse files Browse the repository at this point in the history
* Refs #14367. Closing socket on UDPChannelResource destructor.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs #14367. Do not close socket on UDPChannelResource::release.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs #14367. Bonus: do not call socket->release() on TCP disconnect.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs #14367. Linters.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
(cherry picked from commit 184e982)

Co-authored-by: Miguel Company <miguelcompany@eprosima.com>
  • Loading branch information
mergify[bot] and MiguelCompany authored Apr 28, 2022
1 parent f9bb9df commit 81c1ccb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 0 additions & 5 deletions src/cpp/rtps/transport/TCPChannelResourceBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ void TCPChannelResourceBasic::disconnect()
try
{
socket->cancel();

// This method was added on the version 1.12.0
#if ASIO_VERSION >= 101200 && (!defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x0603)
socket->release();
#endif // if ASIO_VERSION >= 101200 && (!defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x0603)
socket->close();
}
catch (std::exception&)
Expand Down
8 changes: 7 additions & 1 deletion src/cpp/rtps/transport/UDPChannelResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ UDPChannelResource::UDPChannelResource(
UDPChannelResource::~UDPChannelResource()
{
message_receiver_ = nullptr;

asio::error_code ec;
socket()->close(ec);
}

void UDPChannelResource::perform_listen_operation(
Expand Down Expand Up @@ -117,8 +120,11 @@ void UDPChannelResource::release()
// in Windows and Linux anyways, which is what we want.
asio::error_code ec;
socket()->shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
// On OSX shutdown does not unblock the listening thread, but close does.

#if defined(__APPLE__)
// On OSX shutdown does not seem to unblock the listening thread, but close does.
socket()->close();
#endif // if defined(__APPLE__)
}

} // namespace rtps
Expand Down

0 comments on commit 81c1ccb

Please sign in to comment.