Skip to content

Commit 4be1c67

Browse files
committed
undo commit
1 parent b724e39 commit 4be1c67

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/viam/sdk/robot/client.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ void RobotClient::connect_logging() {
116116
}
117117

118118
RobotClient::~RobotClient() {
119-
try {
120-
this->close();
121-
} catch (const std::exception& e) {
122-
VIAM_SDK_LOG(error) << "Received err while closing RobotClient: " << e.what();
123-
} catch (...) {
124-
VIAM_SDK_LOG(error) << "Received unknown err while closing RobotClient";
119+
if (should_close_channel_) {
120+
try {
121+
this->close();
122+
} catch (const std::exception& e) {
123+
VIAM_SDK_LOG(error) << "Received err while closing RobotClient: " << e.what();
124+
} catch (...) {
125+
VIAM_SDK_LOG(error) << "Received unknown err while closing RobotClient";
126+
}
125127
}
126128
}
127129

@@ -254,6 +256,7 @@ void RobotClient::refresh_every() {
254256

255257
RobotClient::RobotClient(ViamChannel channel)
256258
: viam_channel_(std::move(channel)),
259+
should_close_channel_(false),
257260
impl_(std::make_unique<impl>(RobotService::NewStub(viam_channel_.channel()))) {}
258261

259262
std::vector<Name> RobotClient::resource_names() const {
@@ -304,6 +307,7 @@ std::shared_ptr<RobotClient> RobotClient::at_address(const std::string& address,
304307
const char* uri = address.c_str();
305308
auto robot =
306309
RobotClient::with_channel(ViamChannel::dial_initial(uri, options.dial_options()), options);
310+
robot->should_close_channel_ = true;
307311

308312
return robot;
309313
};
@@ -314,6 +318,7 @@ std::shared_ptr<RobotClient> RobotClient::at_local_socket(const std::string& add
314318
auto robot = RobotClient::with_channel(
315319
ViamChannel(sdk::impl::create_viam_channel(addr, grpc::InsecureChannelCredentials())),
316320
options);
321+
robot->should_close_channel_ = true;
317322

318323
return robot;
319324
};

src/viam/sdk/robot/client.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ struct LogBackend;
3232
/// - `RobotClient::at_address(...)`
3333
/// - `RobotClient::with_channel(...)`
3434
/// @ingroup Robot
35+
///
36+
/// You must `close()` a robot when finished with it in order to release its resources.
37+
/// Robots creates via `at_address` will automatically close, but robots created via
38+
/// `with_channel` require a user call to `close()`.
3539
class RobotClient {
3640
public:
3741
/// @enum status
@@ -84,6 +88,8 @@ class RobotClient {
8488
/// @brief Creates a robot client connected to the provided channel.
8589
/// @param channel The channel to connect with.
8690
/// @param options Options for connecting and refreshing.
91+
/// Connects directly to a pre-existing channel. A robot created this way must be
92+
/// `close()`d manually.
8793
static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
8894

8995
std::vector<Name> resource_names() const;
@@ -165,6 +171,7 @@ class RobotClient {
165171
unsigned int refresh_interval_;
166172

167173
ViamChannel viam_channel_;
174+
bool should_close_channel_;
168175

169176
struct impl;
170177
std::unique_ptr<impl> impl_;

src/viam/sdk/tests/test_robot.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ void robot_client_to_mocks_pipeline(F&& test_case) {
5555
// Run the passed-in test case on the created stack and give access to the
5656
// created RobotClient and MockRobotService.
5757
std::forward<F>(test_case)(client, service);
58+
59+
// Shutdown Server afterward.
60+
server->shutdown();
5861
}
5962

6063
BOOST_AUTO_TEST_CASE(test_registering_resources) {

0 commit comments

Comments
 (0)