From 8eba2f31ab2123b6197392ac91b291b531b7ece6 Mon Sep 17 00:00:00 2001 From: CihatAltiparmak Date: Mon, 29 Jul 2024 08:30:54 +0300 Subject: [PATCH 1/4] Added SIGTERM handler for linux and closed session when reading error from tty --- rmw_zenoh_cpp/src/zenohd/main.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rmw_zenoh_cpp/src/zenohd/main.cpp b/rmw_zenoh_cpp/src/zenohd/main.cpp index fd749d4b..afd51883 100644 --- a/rmw_zenoh_cpp/src/zenohd/main.cpp +++ b/rmw_zenoh_cpp/src/zenohd/main.cpp @@ -204,6 +204,7 @@ int main(int argc, char ** argv) SetConsoleCtrlHandler(quit, TRUE); #else signal(SIGINT, quit); + signal(SIGTERM, quit); #endif KeyboardReader keyreader; @@ -217,6 +218,8 @@ int main(int argc, char ** argv) c = keyreader.readOne(); } catch (const std::runtime_error &) { perror("read():"); + + z_close(z_move(s)); return -1; } From 0566dc2a4736931e8c61c3bd370cee78ee07716b Mon Sep 17 00:00:00 2001 From: CihatAltiparmak Date: Wed, 31 Jul 2024 11:30:46 +0300 Subject: [PATCH 2/4] Added scope_exit for closing session --- rmw_zenoh_cpp/src/zenohd/main.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rmw_zenoh_cpp/src/zenohd/main.cpp b/rmw_zenoh_cpp/src/zenohd/main.cpp index afd51883..0227b9d4 100644 --- a/rmw_zenoh_cpp/src/zenohd/main.cpp +++ b/rmw_zenoh_cpp/src/zenohd/main.cpp @@ -36,6 +36,8 @@ #include "rmw/error_handling.h" +#include "rcpputils/scope_exit.hpp" + static bool running = true; class KeyboardReader final @@ -192,14 +194,14 @@ int main(int argc, char ** argv) return 1; } - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { printf("Unable to open router session!\n"); return 1; } printf( "Started Zenoh router with id %s.\n", - rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&s))).c_str()); + rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&session))).c_str()); #ifdef _WIN32 SetConsoleCtrlHandler(quit, TRUE); #else @@ -207,6 +209,11 @@ int main(int argc, char ** argv) signal(SIGTERM, quit); #endif + auto close_session = rcpputils::make_scope_exit( + [&session]() { + z_close(z_move(session)); + }); + KeyboardReader keyreader; char c = 0; @@ -218,8 +225,6 @@ int main(int argc, char ** argv) c = keyreader.readOne(); } catch (const std::runtime_error &) { perror("read():"); - - z_close(z_move(s)); return -1; } @@ -230,7 +235,5 @@ int main(int argc, char ** argv) std::this_thread::sleep_for(std::chrono::milliseconds(100)); } - z_close(z_move(s)); - return 0; } From 35c5ba0fb81878796ab75bfa705f241daf87fd1d Mon Sep 17 00:00:00 2001 From: CihatAltiparmak Date: Fri, 2 Aug 2024 11:39:12 +0300 Subject: [PATCH 3/4] Moved scope_exit right after checking session --- rmw_zenoh_cpp/src/zenohd/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rmw_zenoh_cpp/src/zenohd/main.cpp b/rmw_zenoh_cpp/src/zenohd/main.cpp index 0227b9d4..233850bb 100644 --- a/rmw_zenoh_cpp/src/zenohd/main.cpp +++ b/rmw_zenoh_cpp/src/zenohd/main.cpp @@ -199,6 +199,11 @@ int main(int argc, char ** argv) printf("Unable to open router session!\n"); return 1; } + auto close_session = rcpputils::make_scope_exit( + [&session]() { + z_close(z_move(session)); + }); + printf( "Started Zenoh router with id %s.\n", rmw_zenoh_cpp::liveliness::zid_to_str(z_info_zid(z_session_loan(&session))).c_str()); @@ -209,11 +214,6 @@ int main(int argc, char ** argv) signal(SIGTERM, quit); #endif - auto close_session = rcpputils::make_scope_exit( - [&session]() { - z_close(z_move(session)); - }); - KeyboardReader keyreader; char c = 0; From e749b1136d25fe65ca1dca10e0977d1ddb9e2f98 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Tue, 6 Aug 2024 02:49:52 +0800 Subject: [PATCH 4/4] Minor tweak to scope_exit name Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/zenohd/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/zenohd/main.cpp b/rmw_zenoh_cpp/src/zenohd/main.cpp index 233850bb..fe6241be 100644 --- a/rmw_zenoh_cpp/src/zenohd/main.cpp +++ b/rmw_zenoh_cpp/src/zenohd/main.cpp @@ -199,7 +199,7 @@ int main(int argc, char ** argv) printf("Unable to open router session!\n"); return 1; } - auto close_session = rcpputils::make_scope_exit( + auto always_close_session = rcpputils::make_scope_exit( [&session]() { z_close(z_move(session)); });