Skip to content

Commit ca5058c

Browse files
Another fix for ClientData. (#336)
* Another fix for ClientData. Signed-off-by: Chris Lalancette <[email protected]> * Minor optimizations Signed-off-by: Yadunund <[email protected]> --------- Signed-off-by: Chris Lalancette <[email protected]> Signed-off-by: Yadunund <[email protected]> Co-authored-by: Yadunund <[email protected]>
1 parent 8bca9d7 commit ca5058c

File tree

3 files changed

+25
-66
lines changed

3 files changed

+25
-66
lines changed

rmw_zenoh_cpp/src/detail/rmw_client_data.cpp

+24-49
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,30 @@
4242

4343
namespace
4444
{
45+
///=============================================================================
46+
struct ClientDataWrapper
47+
{
48+
explicit ClientDataWrapper(std::shared_ptr<rmw_zenoh_cpp::ClientData> data)
49+
: client_data(std::move(data))
50+
{
51+
}
52+
53+
std::shared_ptr<rmw_zenoh_cpp::ClientData> client_data;
54+
};
4555

4656
///=============================================================================
4757
void client_data_handler(z_loaned_reply_t * reply, void * data)
4858
{
49-
auto client_data = static_cast<rmw_zenoh_cpp::ClientData *>(data);
50-
if (client_data == nullptr) {
59+
auto wrapper = static_cast<ClientDataWrapper *>(data);
60+
if (wrapper == nullptr) {
5161
RMW_ZENOH_LOG_ERROR_NAMED(
5262
"rmw_zenoh_cpp",
5363
"Unable to obtain client_data_t from data in client_data_handler."
5464
);
5565
return;
5666
}
5767

58-
if (client_data->is_shutdown()) {
68+
if (wrapper->client_data->is_shutdown()) {
5969
return;
6070
}
6171

@@ -69,7 +79,7 @@ void client_data_handler(z_loaned_reply_t * reply, void * data)
6979
RMW_ZENOH_LOG_ERROR_NAMED(
7080
"rmw_zenoh_cpp",
7181
"z_reply_is_ok returned False for keyexpr %s. Reason: %.*s",
72-
client_data->topic_info().topic_keyexpr_.c_str(),
82+
wrapper->client_data->topic_info().topic_keyexpr_.c_str(),
7383
static_cast<int>(z_string_len(z_loan(err_str))),
7484
z_string_data(z_loan(err_str)));
7585
z_drop(z_move(err_str));
@@ -80,23 +90,23 @@ void client_data_handler(z_loaned_reply_t * reply, void * data)
8090
std::chrono::nanoseconds::rep received_timestamp =
8191
std::chrono::system_clock::now().time_since_epoch().count();
8292

83-
client_data->add_new_reply(
93+
wrapper->client_data->add_new_reply(
8494
std::make_unique<rmw_zenoh_cpp::ZenohReply>(reply, received_timestamp));
8595
}
8696

8797
///=============================================================================
8898
void client_data_drop(void * data)
8999
{
90-
auto client_data = static_cast<rmw_zenoh_cpp::ClientData *>(data);
91-
if (client_data == nullptr) {
100+
auto wrapper = static_cast<ClientDataWrapper *>(data);
101+
if (wrapper == nullptr) {
92102
RMW_ZENOH_LOG_ERROR_NAMED(
93103
"rmw_zenoh_cpp",
94104
"Unable to obtain client_data_t from data in client_data_drop."
95105
);
96106
return;
97107
}
98108

99-
client_data->decrement_in_flight_and_conditionally_remove();
109+
delete wrapper;
100110
}
101111

102112
} // namespace
@@ -228,8 +238,7 @@ ClientData::ClientData(
228238
wait_set_data_(nullptr),
229239
sequence_number_(1),
230240
is_shutdown_(false),
231-
initialized_(false),
232-
num_in_flight_(0)
241+
initialized_(false)
233242
{
234243
// Do nothing.
235244
}
@@ -470,9 +479,9 @@ rmw_ret_t ClientData::send_request(
470479

471480
// TODO(Yadunund): Once we switch to zenoh-cpp with lambda closures,
472481
// capture shared_from_this() instead of this.
473-
num_in_flight_++;
482+
ClientDataWrapper * wrapper = new ClientDataWrapper(shared_from_this());
474483
z_owned_closure_reply_t zn_closure_reply;
475-
z_closure(&zn_closure_reply, client_data_handler, client_data_drop, this);
484+
z_closure(&zn_closure_reply, client_data_handler, client_data_drop, wrapper);
476485
z_get(
477486
sess_->loan(),
478487
z_loan(keyexpr_), "",
@@ -527,10 +536,11 @@ bool ClientData::detach_condition_and_queue_is_empty()
527536
}
528537

529538
///=============================================================================
530-
void ClientData::_shutdown()
539+
rmw_ret_t ClientData::shutdown()
531540
{
541+
std::lock_guard<std::recursive_mutex> lock(mutex_);
532542
if (is_shutdown_) {
533-
return;
543+
return RMW_RET_OK;
534544
}
535545

536546
// Unregister this node from the ROS graph.
@@ -541,45 +551,10 @@ void ClientData::_shutdown()
541551

542552
sess_.reset();
543553
is_shutdown_ = true;
544-
}
545554

546-
///=============================================================================
547-
rmw_ret_t ClientData::shutdown()
548-
{
549-
std::lock_guard<std::recursive_mutex> lock(mutex_);
550-
_shutdown();
551555
return RMW_RET_OK;
552556
}
553557

554-
///=============================================================================
555-
bool ClientData::shutdown_and_query_in_flight()
556-
{
557-
std::lock_guard<std::recursive_mutex> lock(mutex_);
558-
_shutdown();
559-
return num_in_flight_ > 0;
560-
}
561-
562-
///=============================================================================
563-
void ClientData::decrement_in_flight_and_conditionally_remove()
564-
{
565-
std::unique_lock<std::recursive_mutex> lock(mutex_);
566-
--num_in_flight_;
567-
568-
if (is_shutdown_ && num_in_flight_ == 0) {
569-
rmw_context_impl_s * context_impl = static_cast<rmw_context_impl_s *>(rmw_node_->data);
570-
if (context_impl == nullptr) {
571-
return;
572-
}
573-
std::shared_ptr<rmw_zenoh_cpp::NodeData> node_data = context_impl->get_node_data(rmw_node_);
574-
if (node_data == nullptr) {
575-
return;
576-
}
577-
// We have to unlock here since we are about to delete ourself, and thus the unlock would be UB.
578-
lock.unlock();
579-
node_data->delete_client_data(rmw_client_);
580-
}
581-
}
582-
583558
///=============================================================================
584559
bool ClientData::is_shutdown() const
585560
{

rmw_zenoh_cpp/src/detail/rmw_client_data.hpp

-10
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
9595
// Shutdown this ClientData.
9696
rmw_ret_t shutdown();
9797

98-
// Shutdown this ClientData, and return whether there are any requests currently in flight.
99-
bool shutdown_and_query_in_flight();
100-
101-
// Decrement the in flight requests, and if that drops to 0 remove the client from the node.
102-
void decrement_in_flight_and_conditionally_remove();
103-
10498
// Check if this ClientData is shutdown.
10599
bool is_shutdown() const;
106100

@@ -122,9 +116,6 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
122116
// Initialize the Zenoh objects for this entity.
123117
bool init(std::shared_ptr<ZenohSession> session);
124118

125-
// Shutdown this client (the mutex is expected to be held by the caller).
126-
void _shutdown();
127-
128119
// Internal mutex.
129120
mutable std::recursive_mutex mutex_;
130121
// The parent node.
@@ -155,7 +146,6 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
155146
bool is_shutdown_;
156147
// Whether the object has ever successfully been initialized.
157148
bool initialized_;
158-
size_t num_in_flight_;
159149
};
160150
using ClientDataPtr = std::shared_ptr<ClientData>;
161151
using ClientDataConstPtr = std::shared_ptr<const ClientData>;

rmw_zenoh_cpp/src/detail/rmw_node_data.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,7 @@ ClientDataPtr NodeData::get_client_data(const rmw_client_t * const client)
383383
void NodeData::delete_client_data(const rmw_client_t * const client)
384384
{
385385
std::lock_guard<std::recursive_mutex> lock_guard(mutex_);
386-
auto client_it = clients_.find(client);
387-
if (client_it == clients_.end()) {
388-
return;
389-
}
390-
if (!client_it->second->shutdown_and_query_in_flight()) {
391-
clients_.erase(client);
392-
}
386+
clients_.erase(client);
393387
}
394388

395389
///=============================================================================

0 commit comments

Comments
 (0)