Skip to content

Nolocal close #434

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions include/zenoh/api.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "api/reply.hxx"
#include "api/sample.hxx"
#include "api/scout.hxx"
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
#include "api/close.hxx"
#endif
#include "api/session.hxx"
#include "api/subscriber.hxx"
#include "api/timestamp.hxx"
Expand Down
38 changes: 38 additions & 0 deletions include/zenoh/api/close.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>

#pragma once

#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)

namespace zenoh {

/// A close operation handle.
class CloseHandle : public Owned<::zc_owned_concurrent_close_handle_t> {
friend class Session;
CloseHandle(zenoh::detail::null_object_t) : Owned(nullptr){};

public:
/// @brief Blocks until corresponding close operation completes.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
void wait(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zc_concurrent_close_handle_wait(interop::as_moved_c_ptr(*this)), err,
"Failed to wait for close operation");
}
};

} // namespace zenoh

#endif
26 changes: 26 additions & 0 deletions include/zenoh/api/session.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)
#include "shm/client_storage/client_storage.hxx"
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
#include "close.hxx"
#endif

namespace zenoh {
namespace ext {
Expand Down Expand Up @@ -63,6 +66,14 @@ class Session : public Owned<::z_owned_session_t> {

/// @brief Options to be passed when closing a ``Session``.
struct SessionCloseOptions {
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// The timeout for close operation in milliseconds. 0 means default close timeout which is 10 seconds.
uint32_t timeout_ms = 10000;
/// A function to receive close handle. If set, the close operation will be executed concurrently
/// in separate task, and this function will receive a handle to be used for controlling
/// close execution.
std::function<void(CloseHandle&&)> out_concurrent = nullptr;
Copy link
Contributor

@DenisBiryukov91 DenisBiryukov91 Mar 26, 2025

Choose a reason for hiding this comment

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

why not simply return close handle from close ? Or maybe even better make a separate close_async. Btw why can't we just only pass a timeout and move session close call to a separate async C++ task and just wait on its promise ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd need an overload by return value to do this. And this is not available in C++ :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because there is still a common version of close that doesn't return handle

Copy link
Contributor

Choose a reason for hiding this comment

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

Close can always return handle, just in case of missing timeout it can be NULL, or as I suggested make a separate close_async. Also what is wrong with using C++ async task with blocking close and timeout ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The timeout is not tied to async close, it is ok to use it even with blocking close.

The pros to use "our" async close instead of making std::async:

  • "our" close internals in Rust are covered with tests to work in atexit etc
  • "our" close allows dropping a session once close handle is created and even before the actual close completed

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly, common sense tells that async does not need a timeout (because its non-blocking), so timeout should logically be an option only for a sync call (otherwise it does not make sense at all - is it amount of time that is provided to session to close itself ? then what happens if it does not manage to do it ? - it is not at all clear from the docs).

We can totally keep the behavior of rust, I just suggest to wrap handle into a promise over a simple function that waits on it to avoid creating unnecessary data types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea of close_async, but thus we need also to fix zenoh-c to have similar behavior. I will make this after the release and rmw code freee

#endif
/// @name Fields
static SessionCloseOptions create_default() { return {}; }
};
Expand Down Expand Up @@ -1129,8 +1140,23 @@ class Session : public Owned<::z_owned_session_t> {
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
void close(SessionCloseOptions&& options = SessionCloseOptions::create_default(), ZResult* err = nullptr) {
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
CloseHandle close_handle(zenoh::detail::null_object);
::z_close_options_t close_opts;
::z_close_options_default(&close_opts);
close_opts.internal_timeout_ms = options.timeout_ms;
if (options.out_concurrent) {
close_opts.internal_out_concurrent = &close_handle._0;
}
__ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), &close_opts), err,
"Failed to close the session");
if (options.out_concurrent && (!err || *err == Z_OK)) {
options.out_concurrent(std::move(close_handle));
}
#else
(void)options;
__ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), nullptr), err, "Failed to close the session");
#endif
}

/// @brief Check if session is closed.
Expand Down
54 changes: 54 additions & 0 deletions tests/zenohc/close.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#include "zenoh.hxx"

using namespace zenoh;

#undef NDEBUG
#include <assert.h>

void test_session_close_in_drop() { auto session = Session::open(Config::create_default()); }

void test_session_close() {
auto session = Session::open(Config::create_default());
session.close();
}

#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
void test_session_close_in_background() {
auto session = Session::open(Config::create_default());

bool close_called = false;
auto close_options = Session::SessionCloseOptions::create_default();
close_options.out_concurrent = [&close_called](CloseHandle&& h) {
h.wait();
close_called = true;
};

session.close(std::move(close_options));

if (!close_called) {
exit(-1);
}
}
#endif

int main() {
test_session_close_in_drop();
test_session_close();
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
test_session_close_in_background();
#endif
}
2 changes: 1 addition & 1 deletion zenoh-c