Skip to content

Commit

Permalink
Avoid touching Zenoh Session while exiting. (#37)
Browse files Browse the repository at this point in the history
* Avoid touching Zenoh Session while exiting.

Signed-off-by: ChenYing Kuo <[email protected]>

* Register function right after opening Zenoh Session.

Signed-off-by: ChenYing Kuo <[email protected]>

---------

Signed-off-by: ChenYing Kuo <[email protected]>
  • Loading branch information
evshary authored Oct 31, 2024
1 parent 5c3d385 commit bb8e0ec
Showing 1 changed file with 25 additions and 3 deletions.
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

0 comments on commit bb8e0ec

Please sign in to comment.