@@ -108,10 +108,22 @@ class ServiceIntraProcess : public ServiceIntraProcessBase
108108 return std::static_pointer_cast<void >(data);
109109 }
110110
111- void send_response (uint64_t intra_process_client_id , SharedResponse & response)
111+ void send_response (int64_t client_request_id , SharedResponse & response)
112112 {
113113 std::unique_lock<std::recursive_mutex> lock (reentrant_mutex_);
114114
115+ auto client_request_it = callback_info_.find (client_request_id);
116+
117+ if (client_request_it == callback_info_.end ()) {
118+ RCLCPP_WARN (
119+ rclcpp::get_logger (" rclcpp" ),
120+ " Calling intra_process_service_send_response for invalid or no "
121+ " longer existing request id" );
122+
123+ return ;
124+ }
125+
126+ auto intra_process_client_id = client_request_it->second .first ;
115127 auto client_it = clients_.find (intra_process_client_id);
116128
117129 if (client_it == clients_.end ()) {
@@ -120,7 +132,7 @@ class ServiceIntraProcess : public ServiceIntraProcessBase
120132 " Calling intra_process_service_send_response for invalid or no "
121133 " longer existing client id" );
122134
123- callback_info_.erase (intra_process_client_id );
135+ callback_info_.erase (client_request_id );
124136 return ;
125137 }
126138
@@ -130,14 +142,14 @@ class ServiceIntraProcess : public ServiceIntraProcessBase
130142 auto client = std::dynamic_pointer_cast<
131143 rclcpp::experimental::ClientIntraProcess<ServiceT>>(
132144 client_intra_process_base);
133- CallbackInfoVariant & value = callback_info_[intra_process_client_id] ;
145+ CallbackInfoVariant & value = client_request_it-> second . second ;
134146 client->store_intra_process_response (
135147 std::make_pair (std::move (response), std::move (value)));
136148 } else {
137149 clients_.erase (client_it);
138150 }
139151
140- callback_info_.erase (intra_process_client_id );
152+ callback_info_.erase (client_request_id );
141153 }
142154
143155 void execute (const std::shared_ptr<void > & data)
@@ -154,18 +166,20 @@ class ServiceIntraProcess : public ServiceIntraProcessBase
154166 uint64_t intra_process_client_id = ptr->first ;
155167 SharedRequest & typed_request = ptr->second .first ;
156168 CallbackInfoVariant & value = ptr->second .second ;
157- callback_info_.emplace (std::make_pair (intra_process_client_id, std::move (value)));
158169
159170 // To allow for the user callback to handle deferred responses for IPC in an ambiguous way,
160171 // we are overloading the rmw_request_id semantics to provide the intra process client ID.
172+ uint64_t client_request_id = get_unique_request_id ();
161173 auto req_id = std::make_shared<rmw_request_id_t >();
162- req_id->sequence_number = intra_process_client_id ;
174+ req_id->sequence_number = client_request_id ;
163175 req_id->from_intra_process = true ;
164176
177+ callback_info_.emplace (std::make_pair (req_id->sequence_number , std::make_pair (intra_process_client_id, std::move (value))));
178+
165179 SharedResponse response = any_callback_.dispatch (serv_handle, req_id, std::move (typed_request));
166180
167181 if (response) {
168- send_response (intra_process_client_id , response);
182+ send_response (req_id-> sequence_number , response);
169183 }
170184 }
171185
@@ -182,7 +196,7 @@ class ServiceIntraProcess : public ServiceIntraProcessBase
182196
183197 // Store callback variants in a map to support deferred response
184198 // access by intra-process client id.
185- std::unordered_map<uint64_t , CallbackInfoVariant> callback_info_;
199+ std::unordered_map<int64_t , std::pair< uint64_t , CallbackInfoVariant> > callback_info_;
186200};
187201
188202} // namespace experimental
0 commit comments