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

[Foxy] Handle signals within the asyncio loop. (#476) #506

Merged
merged 11 commits into from
Jul 7, 2021
Prev Previous commit
Next Next commit
Close the socket pair used for signal management (#497)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
ivanpauno authored and hidmic committed Jun 29, 2021
commit 4fd532aa2576e3f5a797e26a0e7d14d9ee52d321
23 changes: 20 additions & 3 deletions launch/launch/utilities/signal_management.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
"""Module for signal management functionality."""

import asyncio
from contextlib import ExitStack
import os
import signal
import socket
@@ -81,16 +82,28 @@ def __init__(
self.__background_loop = None # type: Optional[asyncio.AbstractEventLoop]
self.__handlers = {} # type: dict
self.__prev_wakeup_handle = -1 # type: Union[int, socket.socket]
self.__wsock, self.__rsock = socket.socketpair() # type: Tuple[socket.socket, socket.socket] # noqa
self.__wsock.setblocking(False)
self.__rsock.setblocking(False)
self.__wsock = None
self.__rsock = None
self.__close_sockets = None

def __enter__(self):
pair = socket.socketpair() # type: Tuple[socket.socket, socket.socket] # noqa
with ExitStack() as stack:
self.__wsock = stack.enter_context(pair[0])
self.__rsock = stack.enter_context(pair[1])
self.__wsock.setblocking(False)
self.__rsock.setblocking(False)
self.__close_sockets = stack.pop_all().close

self.__add_signal_readers()
try:
self.__install_signal_writers()
except Exception:
self.__remove_signal_readers()
self.__close_sockets()
self.__rsock = None
self.__wsock = None
self.__close_sockets = None
raise
self.__chain()
return self
@@ -103,6 +116,10 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
self.__remove_signal_readers()
finally:
self.__unchain()
self.__close_sockets()
self.__rsock = None
self.__wsock = None
self.__close_sockets = None

def __add_signal_readers(self):
try: