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

Register our signal handler in rclpy_init vs each wait #194

Merged
merged 3 commits into from
Jun 24, 2018
Merged
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
23 changes: 15 additions & 8 deletions rclpy/src/rclpy/_rclpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ sig_t g_original_signal_handler = NULL;
static void catch_function(int signo)
{
(void) signo;
rcl_ret_t ret = rcl_trigger_guard_condition(g_sigint_gc_handle);
if (ret != RCL_RET_OK) {
PyErr_Format(PyExc_RuntimeError,
"Failed to trigger guard_condition: %s", rcl_get_error_string_safe());
rcl_reset_error();
if (NULL != g_sigint_gc_handle) {
rcl_ret_t ret = rcl_trigger_guard_condition(g_sigint_gc_handle);
if (ret != RCL_RET_OK) {
PyErr_Format(PyExc_RuntimeError,
"Failed to trigger guard_condition: %s", rcl_get_error_string_safe());
rcl_reset_error();
}
}
if (NULL != g_original_signal_handler) {
g_original_signal_handler(signo);
Expand Down Expand Up @@ -425,6 +427,9 @@ rclpy_init(PyObject * Py_UNUSED(self), PyObject * args)
}
Py_DECREF(pyargs);

// Register our signal handler that will forward to the original one.
g_original_signal_handler = signal(SIGINT, catch_function);

if (PyErr_Occurred()) {
return NULL;
}
Expand Down Expand Up @@ -2345,7 +2350,6 @@ rclpy_wait(PyObject * Py_UNUSED(self), PyObject * args)
if (!PyArg_ParseTuple(args, "O|K", &pywait_set, &timeout)) {
return NULL;
}
g_original_signal_handler = signal(SIGINT, catch_function);
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyCapsule_GetPointer(pywait_set, "rcl_wait_set_t");
if (!wait_set) {
return NULL;
Expand All @@ -2357,8 +2361,6 @@ rclpy_wait(PyObject * Py_UNUSED(self), PyObject * args)
ret = rcl_wait(wait_set, timeout);
Py_END_ALLOW_THREADS;

signal(SIGINT, g_original_signal_handler);
g_original_signal_handler = NULL;
if (ret != RCL_RET_OK && ret != RCL_RET_TIMEOUT) {
PyErr_Format(PyExc_RuntimeError,
"Failed to wait on wait set: %s", rcl_get_error_string_safe());
Expand Down Expand Up @@ -2659,6 +2661,11 @@ rclpy_shutdown(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
rcl_reset_error();
return NULL;
}

// Restore the original signal handler.
signal(SIGINT, g_original_signal_handler);
g_original_signal_handler = NULL;

Py_RETURN_NONE;
}

Expand Down