diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp index 12a7374e..7915cd9e 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp @@ -13,8 +13,8 @@ // limitations under the License. #include "rmw_context_impl_s.hpp" -#include +#include #include #include #include @@ -41,16 +41,6 @@ // 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; -} - // This global mapping of raw Data pointers to Data shared pointers allows graph_sub_data_handler() // to lookup the pointer, and gain a reference to a shared_ptr if it exists. // This guarantees that the Data object will not be destroyed while we are using it. @@ -100,13 +90,9 @@ class rmw_context_impl_s::Data final RMW_SET_ERROR_MSG("Error setting up zenoh session."); throw std::runtime_error("Error setting up zenoh session."); } - atexit(update_is_exiting); auto close_session = rcpputils::make_scope_exit( [this]() { - // 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); - } + z_close(z_loan_mut(session_), NULL); }); // Verify if the zenoh router is running if configured. @@ -254,13 +240,10 @@ class rmw_context_impl_s::Data final // to avoid an AB/BA deadlock if shutdown is racing with graph_sub_data_handler(). } - // Don't touch Zenoh Session if the ROS process is exiting, it will cause panic. - if (!is_exiting) { - // Close the zenoh session - if (z_close(z_loan_mut(session_), NULL) != Z_OK) { - RMW_SET_ERROR_MSG("Error while closing zenoh session"); - return RMW_RET_ERROR; - } + // Close the zenoh session + if (z_close(z_loan_mut(session_), NULL) != Z_OK) { + RMW_SET_ERROR_MSG("Error while closing zenoh session"); + return RMW_RET_ERROR; } return RMW_RET_OK; }