-
Notifications
You must be signed in to change notification settings - Fork 46
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
base: main
Are you sure you want to change the base?
Nolocal close #434
Changes from 7 commits
5e32f46
33c95cb
1da68ad
f4a4431
b81edba
e79eeef
59deff1
be3f5fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
|
|
||
| // | ||
| // Copyright (c) 2024 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++ :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because there is still a common version of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 {}; } | ||
| }; | ||
|
|
@@ -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. | ||
|
|
||
| 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 | ||
| } |
| +61 −60 | Cargo.lock | |
| +61 −60 | build-resources/opaque-types/Cargo.lock | |
| +2 −1 | build-resources/opaque-types/src/lib.rs | |
| +4 −6 | src/close.rs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be 2025 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed