@@ -129,11 +129,15 @@ RobotClient::~RobotClient() {
129129
130130void RobotClient::close () {
131131 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 ();
134135 }
136+ threads_.clear ();
137+
135138 stop_all ();
136- viam_channel_->close ();
139+
140+ viam_channel_.close ();
137141}
138142
139143bool is_error_response (const grpc::Status& response) {
@@ -211,7 +215,7 @@ void RobotClient::refresh() {
211215 if (rs) {
212216 try {
213217 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 () );
215219 const Name name_ ({name.namespace_ (), name.type (), name.subtype ()}, " " , name.name ());
216220 new_resources.emplace (name_, rpc_client);
217221 } catch (const std::exception& exc) {
@@ -250,11 +254,10 @@ void RobotClient::refresh_every() {
250254 }
251255};
252256
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)),
256259 should_close_channel_ (false ),
257- impl_(std::make_unique<impl>(RobotService::NewStub(channel_ ))) {}
260+ impl_(std::make_unique<impl>(RobotService::NewStub(viam_channel_.channel() ))) {}
258261
259262std::vector<Name> RobotClient::resource_names () const {
260263 const std::lock_guard<std::mutex> lock (lock_);
@@ -286,20 +289,14 @@ void RobotClient::log(const std::string& name,
286289 }
287290}
288291
289- std::shared_ptr<RobotClient> RobotClient::with_channel (std::shared_ptr< ViamChannel> channel,
292+ std::shared_ptr<RobotClient> RobotClient::with_channel (ViamChannel channel,
290293 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));
292295 robot->refresh_interval_ = options.refresh_interval ();
293296 robot->should_refresh_ = (robot->refresh_interval_ > 0 );
294297 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+ }
303300
304301 robot->refresh ();
305302 return robot;
@@ -308,8 +305,8 @@ std::shared_ptr<RobotClient> RobotClient::with_channel(std::shared_ptr<ViamChann
308305std::shared_ptr<RobotClient> RobotClient::at_address (const std::string& address,
309306 const Options& options) {
310307 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);
313310 robot->should_close_channel_ = true ;
314311
315312 return robot;
@@ -318,11 +315,9 @@ std::shared_ptr<RobotClient> RobotClient::at_address(const std::string& address,
318315std::shared_ptr<RobotClient> RobotClient::at_local_socket (const std::string& address,
319316 const Options& options) {
320317 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);
326321 robot->should_close_channel_ = true ;
327322
328323 return robot;
0 commit comments