Skip to content

Commit

Permalink
Fix callback accessing invalid reference to promise
Browse files Browse the repository at this point in the history
  • Loading branch information
achim-k committed Oct 23, 2023
1 parent a2f88a9 commit 385c4b2
Showing 1 changed file with 6 additions and 1 deletion.
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

0 comments on commit 385c4b2

Please sign in to comment.