Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid touching Zenoh Session while exiting. #37

Merged
merged 2 commits into from
Oct 31, 2024
Merged
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
28 changes: 25 additions & 3 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
// TODO(clalancette): Make this configurable, or get it from the configuration
#define SHM_BUFFER_SIZE_MB 10

// The variable is used to identify whether the process is trying to exit or not.
// The atexit function we registered will set the flag and prevent us from closing
// Zenoh Session. Zenoh API can't be used in atexit function, because Tokio context
// is already destroyed. It will cause panic if we do so.
static bool is_exiting = false;
void update_is_exiting()
{
is_exiting = true;
}

///=============================================================================
void rmw_context_impl_s::graph_sub_data_handler(z_loaned_sample_t * sample, void * data)
{
Expand Down Expand Up @@ -171,7 +181,11 @@ rmw_ret_t rmw_context_impl_s::Data::shutdown()
if (shm_provider_.has_value()) {
z_drop(z_move(shm_provider_.value()));
}
z_close(z_loan_mut(session_), NULL);
// Don't touch Zenoh Session if the ROS process is exiting,
// it will cause panic.
if (!is_exiting) {
z_close(z_loan_mut(session_), NULL);
}
is_shutdown_ = true;
return RMW_RET_OK;
}
Expand Down Expand Up @@ -214,6 +228,10 @@ rmw_context_impl_s::rmw_context_impl_s(
RMW_SET_ERROR_MSG("Error setting up zenoh session");
throw std::runtime_error("Error setting up zenoh session.");
}
// This atexit function is registered after ROS initialization,
// so it should be called before ROS finialization
// Check https://en.cppreference.com/w/cpp/utility/program/exit
atexit(update_is_exiting);
auto close_session = rcpputils::make_scope_exit(
[&session]() {
z_close(z_loan_mut(session), NULL);
Expand Down Expand Up @@ -335,8 +353,12 @@ rmw_context_impl_s::rmw_context_impl_s(
///=============================================================================
rmw_context_impl_s::~rmw_context_impl_s()
{
// std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
// z_drop(z_move(data_->session_));
// Don't touch Zenoh Session if the ROS process is exiting,
// it will cause panic.
if (!is_exiting) {
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
z_drop(z_move(data_->session_));
}
}

///=============================================================================
Expand Down