Skip to content

Commit

Permalink
Added SIGTERM handler for linux and closed session when reading error…
Browse files Browse the repository at this point in the history
… is caught (#252)

* Added SIGTERM handler for linux and closed session when reading error from tty

* Added scope_exit for closing session

* Moved scope_exit right after checking session

* Minor tweak to scope_exit name

Signed-off-by: Yadunund <[email protected]>

---------

Signed-off-by: Yadunund <[email protected]>
Co-authored-by: Yadunund <[email protected]>
  • Loading branch information
CihatAltiparmak and Yadunund authored Aug 5, 2024
1 parent c12ff3e commit a701097
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions rmw_zenoh_cpp/src/zenohd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "rmw/error_handling.h"

#include "rcpputils/scope_exit.hpp"

static bool running = true;

class KeyboardReader final
Expand Down Expand Up @@ -192,18 +194,24 @@ 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;
}
auto always_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(&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
signal(SIGINT, quit);
signal(SIGTERM, quit);
#endif

KeyboardReader keyreader;
Expand All @@ -227,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;
}

0 comments on commit a701097

Please sign in to comment.