Skip to content

Commit

Permalink
refactor: remove the workaround for the atexit shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Nov 28, 2024
1 parent 70990e7 commit 33f0bec
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

#include "rmw_context_impl_s.hpp"
#include <zenoh.h>

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 33f0bec

Please sign in to comment.