Skip to content

Commit

Permalink
Fix freeze in hdl_grabber.cpp (#5826)
Browse files Browse the repository at this point in the history
* Fix freeze in hdl_grabber.cpp

Close the udp socket before stopping HDLGrabber. Tested only on Windows.

* Fix freeze in hdl_grabber.cpp
Address PR comments to fix HDLGrabber freeze on Windows and Linux.

---------

Co-authored-by: SunlayGGX <[email protected]>
  • Loading branch information
SunlayGGX and SunlayGGX authored Nov 14, 2023
1 parent b4c9758 commit a2e6f54
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions io/src/hdl_grabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ pcl::HDLGrabber::stop ()
terminate_read_packet_thread_ = true;
hdl_data_.stopQueue ();

if (hdl_read_socket_ != nullptr)
{
try
{
hdl_read_socket_->shutdown (boost::asio::ip::tcp::socket::shutdown_both);
}
catch (const boost::system::system_error& e)
{
PCL_ERROR("[pcl::HDLGrabber::stop] Failed to shutdown the socket. %s\n", e.what ());
}

hdl_read_socket_->close ();
}

if (hdl_read_packet_thread_ != nullptr)
{
hdl_read_packet_thread_->join ();
Expand Down Expand Up @@ -645,12 +659,21 @@ pcl::HDLGrabber::readPacketsFromSocket ()

while (!terminate_read_packet_thread_ && hdl_read_socket_->is_open ())
{
std::size_t length = hdl_read_socket_->receive_from (boost::asio::buffer (data, 1500), sender_endpoint);
try
{
std::size_t length = hdl_read_socket_->receive_from (boost::asio::buffer (data, 1500), sender_endpoint);

if (isAddressUnspecified (source_address_filter_)
|| (source_address_filter_ == sender_endpoint.address () && source_port_filter_ == sender_endpoint.port ()))
if (isAddressUnspecified (source_address_filter_)
|| (source_address_filter_ == sender_endpoint.address () && source_port_filter_ == sender_endpoint.port ()))
{
enqueueHDLPacket (data, length);
}
}
catch (const boost::system::system_error& e)
{
enqueueHDLPacket (data, length);
// We must ignore those errors triggered on socket close/shutdown request.
if (!(e.code () == boost::asio::error::interrupted || e.code () == boost::asio::error::operation_aborted))
throw;
}
}
}
Expand Down

0 comments on commit a2e6f54

Please sign in to comment.