rclc
rclc copied to clipboard
Proper handling of SIGINT
The system test (https://github.com/ros2/rclc/pull/225) as well as manual tests revealed, that handling of SIGINT doesn't seem to work properly, yet.
That is, when using rclc_executor_spin()
for example, code after rclc_executor_spin()
(e.g. example_service_node.c#L81-L82) is not executed when program is aborted through SIGINT.
The current context check in rclc_executor_spin_some doesn't seem to do the trick.
One option is to expose this feature through an explicit rclc::ok()
, similar to rclcpp::ok()
In rclcpp
, we use signal_handler
to catch the signal SIGINT
and SIGTERM
, which creates a thread that capturing signals, I am wondering that could we use the same method to exit?
I used rclc humble to test if the SIGINT was caught, unfortunatly, the behavior is not as we expected. Here is the test code https://github.com/Zard-C/rclc/blob/3f746c17bea4bd584b246967f9958e4b4baa54b1/rclc_examples/src/example_executor.c#L167
[INFO] [1702829664.805125046] []: Created a timer with period 1000 ms.
Created timer with timeout 1000 ms.
Created subscriber topic_0:
Debug: number of DDS handles: 2
Published message Hello World!
Callback: I heard: Hello World!
^C
Even when I used setvbuf to force flush the output, it still shows that the clean job codes can not be reached.
I am positive for this feature enabled in rclc as well, it can let the program exit elegently. 😸
That's a good point. Currently, this check in rclc_executor_spin_some
is used to stop spinning. It could be extended by catching a SGINT as well, as you mentioned. Or the while(true) condition in rclc_executor_spin
(and other functions) could be replaced. Could you provide a pull request?
The only issue I see is, that rclc cannot rely on pthread
library. It has to be portable for other RTOS, that do not support this thread API. Or an additional abstraction layer is necessary for thread creation.