Skip to content

Commit 73df85e

Browse files
author
ChuckEllison
committed
fix disconnect crash
1 parent 553f1bf commit 73df85e

9 files changed

Lines changed: 34 additions & 11 deletions

File tree

doc/custom-subscriber-plugin.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This tutorial will guide you through the steps to create a simple custom subscri
9090
TestSubscriber::~TestSubscriber()
9191
{
9292
std::scoped_lock lock(subscriber_mutex_);
93+
inactive_ = true;
9394
9495
RCLCPP_INFO(logger_, "TestSubscriber destroyed!");
9596
}
@@ -142,6 +143,8 @@ This tutorial will guide you through the steps to create a simple custom subscri
142143
{
143144
std::scoped_lock lock(subscriber_mutex_);
144145
146+
if(inactive_) return;
147+
145148
RCLCPP_INFO_STREAM(logger_, "New TestSubscriber msg: " << input_msg->data);
146149
147150
// Convert input msg to image
@@ -153,7 +156,7 @@ This tutorial will guide you through the steps to create a simple custom subscri
153156
sensor_msgs::msg::Image output_msg;
154157
bridge_image.toImageMsg(output_msg);
155158
sensor_msgs::msg::Image::ConstSharedPtr output_ptr = std::make_shared<sensor_msgs::msg::Image>(output_msg);
156-
callback_(output_ptr);
159+
try_forward_image(output_ptr);
157160
}
158161
159162
std::shared_ptr<web_video_server::SubscriberInterface> TestSubscriberFactory::create_subscriber(

include/web_video_server/streamer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class StreamerBase : public StreamerInterface
8989
std::map<std::string, std::shared_ptr<SubscriberFactoryInterface>> & subscriber_factories,
9090
rclcpp::Node::WeakPtr node,
9191
std::string logger_name = "streamer");
92+
93+
std::mutex send_mutex_;
9294

9395
bool is_inactive() override
9496
{

include/web_video_server/streamers/image_streamer.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
#include <chrono>
3333
#include <memory>
34-
#include <mutex>
3534
#include <string>
3635
#include <vector>
3736

@@ -80,7 +79,6 @@ class ImageStreamerBase : public StreamerBase
8079

8180
std::chrono::steady_clock::time_point last_frame_;
8281
cv::Mat output_size_image_;
83-
std::mutex send_mutex_;
8482

8583
private:
8684
bool initialized_;

include/web_video_server/streamers/libav_streamer.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class LibavStreamerBase : public ImageStreamerBase
7474
const std::string & format_name,
7575
const std::string & codec_name,
7676
const std::string & content_type);
77-
7877
~LibavStreamerBase();
7978

8079
protected:

include/web_video_server/subscriber.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,21 @@ class SubscriberBase : public SubscriberInterface
8181
const std::string & topic,
8282
const ImageCallback & callback);
8383

84-
protected:
84+
void try_forward_image(const sensor_msgs::msg::Image::ConstSharedPtr &input_msg)
85+
{
86+
try {
87+
callback_(input_msg);
88+
} catch (...) {
89+
RCLCPP_ERROR(logger_, "The subscriber plugin failed send image for some reason.");
90+
}
91+
}
92+
93+
protected:
8594
void subscriberCallback(const sensor_msgs::msg::Image::ConstSharedPtr &input_msg);
8695

8796
rclcpp::Node::SharedPtr node_;
8897
rclcpp::Logger logger_;
98+
bool inactive_;
8999

90100
ImageCallback callback_;
91101
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr sub_;

src/subscriber.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ SubscriberBase::SubscriberBase(
4949
std::string logger_name)
5050
: node_(node)
5151
, logger_(node->get_logger().get_child(logger_name))
52+
, inactive_(false)
5253
{
5354
}
5455

@@ -87,7 +88,8 @@ void SubscriberBase::subscribe(
8788
void SubscriberBase::subscriberCallback(const sensor_msgs::msg::Image::ConstSharedPtr &input_msg)
8889
{
8990
std::scoped_lock lock(subscriber_mutex_);
90-
callback_(input_msg);
91+
92+
try_forward_image(input_msg);
9193
}
9294

9395
std::vector<std::string> SubscriberFactoryInterface::get_available_topics(

src/subscribers/image_transport_subscriber.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ ImageTransportSubscriber::ImageTransportSubscriber(rclcpp::Node::SharedPtr _node
4949

5050
ImageTransportSubscriber::~ImageTransportSubscriber()
5151
{
52-
std::scoped_lock lock(subscriber_mutex_);
52+
std::scoped_lock lock(subscriber_mutex_);
53+
inactive_ = true;
5354
}
5455

5556
void ImageTransportSubscriber::subscribe(const async_web_server_cpp::HttpRequest &request,
@@ -88,8 +89,10 @@ void ImageTransportSubscriber::subscribe(const async_web_server_cpp::HttpRequest
8889
void ImageTransportSubscriber::subscriberCallback(const sensor_msgs::msg::Image::ConstSharedPtr &input_msg)
8990
{
9091
std::scoped_lock lock(subscriber_mutex_);
92+
93+
if(inactive_) return;
9194

92-
callback_(input_msg);
95+
try_forward_image(input_msg);
9396
}
9497

9598
std::shared_ptr<SubscriberInterface> ImageTransportSubscriberFactory::create_subscriber(

src/subscribers/pointcloud2_subscriber.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ PointCloud2Subscriber::PointCloud2Subscriber(rclcpp::Node::SharedPtr node)
6161

6262
PointCloud2Subscriber::~PointCloud2Subscriber()
6363
{
64-
std::scoped_lock lock(subscriber_mutex_);
64+
std::scoped_lock lock(subscriber_mutex_);
65+
inactive_ = true;
6566
}
6667

6768
void PointCloud2Subscriber::subscribe(const async_web_server_cpp::HttpRequest &request,
@@ -125,6 +126,8 @@ void PointCloud2Subscriber::subscriberCallback(const sensor_msgs::msg::PointClou
125126
{
126127
std::scoped_lock lock(subscriber_mutex_);
127128

129+
if(inactive_) return;
130+
128131
if (input_msg->data.size() == 0)
129132
{
130133
RCLCPP_WARN_STREAM_THROTTLE(logger_, *node_->get_clock(), 1000, "No data in pointcloud!");
@@ -300,7 +303,7 @@ void PointCloud2Subscriber::subscriberCallback(const sensor_msgs::msg::PointClou
300303
sensor_msgs::msg::Image output_msg;
301304
colorImage.toImageMsg(output_msg);
302305
sensor_msgs::msg::Image::ConstSharedPtr output_ptr = std::make_shared<sensor_msgs::msg::Image>(output_msg);
303-
callback_(output_ptr);
306+
try_forward_image(output_ptr);
304307

305308
return;
306309
}

src/subscribers/test_subscriber.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TestSubscriber::~TestSubscriber()
1818
{
1919
std::scoped_lock lock(subscriber_mutex_);
20+
inactive_ = true;
2021

2122
RCLCPP_INFO(logger_, "TestSubscriber destroyed!");
2223
}
@@ -69,6 +70,8 @@
6970
{
7071
std::scoped_lock lock(subscriber_mutex_);
7172

73+
if(inactive_) return;
74+
7275
RCLCPP_INFO_STREAM(logger_, "New TestSubscriber msg: " << input_msg->data);
7376

7477
// Convert input msg to image
@@ -80,7 +83,7 @@
8083
sensor_msgs::msg::Image output_msg;
8184
bridge_image.toImageMsg(output_msg);
8285
sensor_msgs::msg::Image::ConstSharedPtr output_ptr = std::make_shared<sensor_msgs::msg::Image>(output_msg);
83-
callback_(output_ptr);
86+
try_forward_image(output_ptr);
8487
}
8588

8689
std::shared_ptr<web_video_server::SubscriberInterface> TestSubscriberFactory::create_subscriber(

0 commit comments

Comments
 (0)