-
Notifications
You must be signed in to change notification settings - Fork 224
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
rclpy Context.on_shutdown callbacks are not called when Ctrl-C is pressed #532
Comments
According to the latest codes (77949d2), this problem also exists. After receiving SIGINT/SIGTERM, this thread will be awaked and call rclpy/rclpy/src/rclpy/signal_handler.cpp Lines 137 to 146 in 77949d2
For user codes ...
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
except ExternalShutdownException:
sys.exit(1)
finally:
rclpy.try_shutdown()
node.destroy_node() It will call Lines 105 to 113 in 77949d2
Maybe we can use setting shutdown flag instead of calling rclpy/rclpy/rclpy/executors.py Lines 588 to 592 in 77949d2
Of course, this may introduce new issues. @ivanpauno do you have any opinions ? |
I don't understand how that would work. Maybe |
My rough idea is At context.cpp, add 2 functions. void requst_shutdown()
{
Set a global shutdown flag
}
bool check_shutdown_request()
{
return global shutdown flag
} At signal_handler.cpp setup_deferred_signal_handler()
{
...
rclpy::requst_shutdown() // call new function
trigger_guard_conditions();
// rclpy::shutdown_contexts(); // comment this line
} At executors.py def _wait_for_ready_callbacks()
...
# Wait for something to become ready
wait_set.wait(timeout_nsec)
if self._is_shutdown:
raise ShutdownException()
if _rclpy.check_shutdown_request or not self._context.ok(): # Check if shutdown is requested.
raise ExternalShutdownException() User app take charge of calling shutdown. ...
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
except ExternalShutdownException:
pass
finally:
rclpy.try_shutdown() |
That would only be effective if there's an executor spinning, if that's not the case |
Yes. I missed this case. |
As I read this issue to be only related to SIGINT (Ctrl+C) just the note for whoever is also wondering: A callback registered via |
The on_shutdown doesn't get triggered unless the user explicitly catches the Ctrl-C and then calls shutdown themselves.
If you put a try catch around the spin it will work. But otherwise you get no on_shutdown which could be useful for cleaning up a hardware driver etc.
Letting the Ctrl-C bubble up is probably still correct, but the Context could use a ContextManager or other mechanism to make sure that the shutdown occurs for this and other unexpected exits otherwise the on_shutdown callback is not particularly useful.
The workaround in the short term is to simply put a try catch around the spin and catch the
KeyboardInterrupt
exception.The text was updated successfully, but these errors were encountered: