@@ -129,11 +129,15 @@ RobotClient::~RobotClient() {
129
129
130
130
void RobotClient::close () {
131
131
should_refresh_.store (false );
132
- for (const std::shared_ptr<std::thread>& t : threads_) {
133
- t->~thread ();
132
+
133
+ for (auto & thread : threads_) {
134
+ thread.join ();
134
135
}
136
+ threads_.clear ();
137
+
135
138
stop_all ();
136
- viam_channel_->close ();
139
+
140
+ viam_channel_.close ();
137
141
}
138
142
139
143
bool is_error_response (const grpc::Status& response) {
@@ -211,7 +215,7 @@ void RobotClient::refresh() {
211
215
if (rs) {
212
216
try {
213
217
const std::shared_ptr<Resource> rpc_client =
214
- rs->create_rpc_client (name.name (), channel_ );
218
+ rs->create_rpc_client (name.name (), viam_channel_. channel () );
215
219
const Name name_ ({name.namespace_ (), name.type (), name.subtype ()}, " " , name.name ());
216
220
new_resources.emplace (name_, rpc_client);
217
221
} catch (const std::exception & exc) {
@@ -250,11 +254,10 @@ void RobotClient::refresh_every() {
250
254
}
251
255
};
252
256
253
- RobotClient::RobotClient (std::shared_ptr<ViamChannel> channel)
254
- : channel_(channel->channel ()),
255
- viam_channel_(std::move(channel)),
257
+ RobotClient::RobotClient (ViamChannel channel)
258
+ : viam_channel_(std::move(channel)),
256
259
should_close_channel_ (false ),
257
- impl_(std::make_unique<impl>(RobotService::NewStub(channel_ ))) {}
260
+ impl_(std::make_unique<impl>(RobotService::NewStub(viam_channel_.channel() ))) {}
258
261
259
262
std::vector<Name> RobotClient::resource_names () const {
260
263
const std::lock_guard<std::mutex> lock (lock_);
@@ -286,20 +289,14 @@ void RobotClient::log(const std::string& name,
286
289
}
287
290
}
288
291
289
- std::shared_ptr<RobotClient> RobotClient::with_channel (std::shared_ptr< ViamChannel> channel,
292
+ std::shared_ptr<RobotClient> RobotClient::with_channel (ViamChannel channel,
290
293
const Options& options) {
291
- std::shared_ptr<RobotClient> robot = std::make_shared<RobotClient>(std::move (channel));
294
+ auto robot = std::make_shared<RobotClient>(std::move (channel));
292
295
robot->refresh_interval_ = options.refresh_interval ();
293
296
robot->should_refresh_ = (robot->refresh_interval_ > 0 );
294
297
if (robot->should_refresh_ ) {
295
- const std::shared_ptr<std::thread> t =
296
- std::make_shared<std::thread>(&RobotClient::refresh_every, robot);
297
- // TODO(RSDK-1743): this was leaking, confirm that adding thread catching in
298
- // close/destructor lets us shutdown gracefully. See also address sanitizer,
299
- // UB sanitizer
300
- t->detach ();
301
- robot->threads_ .push_back (t);
302
- };
298
+ robot->threads_ .emplace_back (&RobotClient::refresh_every, robot);
299
+ }
303
300
304
301
robot->refresh ();
305
302
return robot;
@@ -308,8 +305,8 @@ std::shared_ptr<RobotClient> RobotClient::with_channel(std::shared_ptr<ViamChann
308
305
std::shared_ptr<RobotClient> RobotClient::at_address (const std::string& address,
309
306
const Options& options) {
310
307
const char * uri = address.c_str ();
311
- auto channel = ViamChannel::dial_initial (uri, options. dial_options ());
312
- std::shared_ptr<RobotClient> robot = RobotClient::with_channel (channel , options);
308
+ auto robot =
309
+ RobotClient::with_channel (ViamChannel::dial_initial (uri, options. dial_options ()) , options);
313
310
robot->should_close_channel_ = true ;
314
311
315
312
return robot;
@@ -318,11 +315,9 @@ std::shared_ptr<RobotClient> RobotClient::at_address(const std::string& address,
318
315
std::shared_ptr<RobotClient> RobotClient::at_local_socket (const std::string& address,
319
316
const Options& options) {
320
317
const std::string addr = " unix://" + address;
321
- const char * uri = addr.c_str ();
322
- const std::shared_ptr<grpc::Channel> channel =
323
- sdk::impl::create_viam_channel (uri, grpc::InsecureChannelCredentials ());
324
- auto viam_channel = std::make_shared<ViamChannel>(channel, address.c_str (), nullptr );
325
- std::shared_ptr<RobotClient> robot = RobotClient::with_channel (viam_channel, options);
318
+ auto robot = RobotClient::with_channel (
319
+ ViamChannel (sdk::impl::create_viam_channel (addr, grpc::InsecureChannelCredentials ())),
320
+ options);
326
321
robot->should_close_channel_ = true ;
327
322
328
323
return robot;
0 commit comments