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 callback accessing invalid reference to promise #268

Merged
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
7 changes: 6 additions & 1 deletion ros1_foxglove_bridge/src/service_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ std::string retrieveServiceType(const std::string& serviceName, std::chrono::mil
auto future = promise.get_future();

link->getConnection()->setHeaderReceivedCallback(
[&promise](const ros::ConnectionPtr&, const ros::Header& header) {
[&promise](const ros::ConnectionPtr& conn, const ros::Header& header) {
std::string serviceType;
if (header.getValue("type", serviceType)) {
promise.set_value(serviceType);
} else {
promise.set_exception(std::make_exception_ptr(
std::runtime_error("Key 'type' not found in service connection header")));
}
// Close connection since we don't need it any more.
conn->drop(ros::Connection::DropReason::Destructing);
return true;
});

if (future.wait_for(timeout) != std::future_status::ready) {
// Drop connection here, removes the link from the service manager instance and avoids
// that the header-received callback is called after the promise has already been destroyed.
link->getConnection()->drop(ros::Connection::DropReason::Destructing);
throw std::runtime_error("Timed out when retrieving service type");
}

Expand Down