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

Close the Zenoh session after destroying the last node #21

Closed
wants to merge 1 commit into from
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
7 changes: 7 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -52,6 +53,12 @@ class rmw_context_impl_s final
// An owned session.
z_owned_session_t session;

// This is a temporary workaround for the unsoundness of rmw_shutdown
// reported in https://github.com/ros2/rmw_zenoh/issues/170.
// We keep track of all the nodes created in this session and only close
// the Zenoh session once the last node is destroyed.
std::unordered_set<const rmw_node_t *> session_nodes_;

// An optional SHM provider that is initialized of SHM is enabled in the
// zenoh session config.
std::optional<z_owned_shm_provider_t> shm_provider;
Expand Down
5 changes: 0 additions & 5 deletions rmw_zenoh_cpp/src/rmw_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,6 @@ rmw_shutdown(rmw_context_t * context)
if (context->impl->shm_provider.has_value()) {
z_drop(z_move(context->impl->shm_provider.value()));
}
// Close the zenoh session
if (z_close(z_move(context->impl->session), NULL) != Z_OK) {
RMW_SET_ERROR_MSG("Error while closing zenoh session");
return RMW_RET_ERROR;
}

context->impl->is_shutdown = true;

Expand Down
14 changes: 14 additions & 0 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ rmw_create_node(
node->context = context;
node->data = node_data;

// Add this node to the set of session_nodes.
context->impl->session_nodes_.insert(node);

free_token.cancel();
free_node_data.cancel();
destruct_node_data.cancel();
Expand Down Expand Up @@ -320,6 +323,17 @@ rmw_destroy_node(rmw_node_t * node)
allocator->deallocate(node_data, allocator->state);
}

// Erase the node from the set of session_nodes and close the Zenoh
// session if this is the last node.
node->context->impl->session_nodes_.erase(node);
if (node->context->impl->session_nodes_.empty()) {
Copy link

@JEnoch JEnoch Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you also test if node->context->impl->is_shutdown == true ?
I mean could it happen that some process close all its Nodes without calling rcl_shutdown(), but re-creating Nodes later ?

👆My bad: on a graceful closure, the Nodes are deleted before calling rcl_shutdown(), and is_shutdown is always false on last node removal.

// Close the zenoh session
if (z_close(z_move(node->context->impl->session), NULL) != Z_OK) {
RMW_SET_ERROR_MSG("Error while closing zenoh session");
return RMW_RET_ERROR;
}
}

allocator->deallocate(const_cast<char *>(node->namespace_), allocator->state);
allocator->deallocate(const_cast<char *>(node->name), allocator->state);
allocator->deallocate(node, allocator->state);
Expand Down