Compare commits

...

1 Commits

Author SHA1 Message Date
Tomoya Fujita
36c77f2d34 Reapply "Catch the exception from rate.sleep() if the context is invalid. (#2956)" (#2963)
This reverts commit 6fbf03945f.

Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
2025-10-06 11:23:23 +09:00
2 changed files with 16 additions and 1 deletions

View File

@@ -67,7 +67,13 @@ Rate::sleep()
// Calculate the time to sleep
auto time_to_sleep = next_interval - now;
// Sleep (will get interrupted by ctrl-c, may not sleep full time)
clock_->sleep_for(time_to_sleep);
try {
// If the context is invalid, an exception will be thrown.
clock_->sleep_for(time_to_sleep);
} catch (const std::runtime_error & e) {
// If it didn't sleep the full time, return false
return false;
}
return true;
}

View File

@@ -173,3 +173,12 @@ TEST_F(TestRate, incorrect_constuctor) {
rclcpp::Rate rate(rclcpp::Duration(-1, 0)),
std::invalid_argument("period must be greater than 0"));
}
TEST(TestRateBasic, invalid_context) {
rclcpp::init(0, nullptr);
rclcpp::Rate rate(1.0);
ASSERT_TRUE(rate.sleep());
rclcpp::shutdown();
EXPECT_NO_THROW(rate.sleep());
ASSERT_FALSE(rate.sleep());
}