Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rclcpp/include/rclcpp/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <functional>
#include <memory>
#include <mutex>
#include <atomic>
#include <string>
#include <typeindex>
#include <typeinfo>
Expand Down Expand Up @@ -365,6 +366,7 @@ class Context : public std::enable_shared_from_this<Context>
std::shared_ptr<rcl_context_t> rcl_context_;
rclcpp::InitOptions init_options_;
std::string shutdown_reason_;
std::atomic<bool> is_shutting_down_{false};

// Keep shared ownership of the global logging mutex.
std::shared_ptr<std::recursive_mutex> logging_mutex_;
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/src/rclcpp/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ Context::shutdown_reason() const
bool
Context::shutdown(const std::string & reason)
{
// Prevent double-shutdown: the signal handler thread and the main thread
// may both call shutdown() during Ctrl-C. Use an atomic flag to ensure
// only the first call proceeds; subsequent calls return immediately.
if (is_shutting_down_.exchange(true)) {
return false;
}
// prevent races
std::lock_guard<std::recursive_mutex> init_lock(init_mutex_);
// ensure validity
Expand Down