Skip to content

Commit 9367e65

Browse files
committed
Replace individual handler functions with single generic one.
Allows arbitrary signal handlers to easily be added in future without writing new functions, DRY. Also allows at least one signal of each type, printing a warning when more than one is received. This is to allow in future each signal type to be passed to the children of launch, so a user can manually escalate the kill signals. Signed-off-by: Cian Donovan <[email protected]>
1 parent 457159a commit 9367e65

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

launch/launch/launch_service.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,34 +186,25 @@ def _prepare_run_loop(self):
186186

187187
self.__this_task = this_task
188188
# Setup custom signal handlers for SIGINT & SIGTERM
189-
sigint_received = False
189+
signals_received: dict = {}
190190

191-
def _on_sigint(signum):
192-
nonlocal sigint_received
193-
base_msg = 'user interrupted with ctrl-c (SIGINT)'
194-
if not sigint_received:
191+
def _on_signal(signum):
192+
nonlocal signals_received
193+
base_msg = 'caught ' + signal.Signals(signum).name
194+
if signum not in signals_received or signals_received[signum] is not True:
195+
signals_received[signum] = True
195196
self.__logger.warning(base_msg)
196197
ret = self._shutdown(
197-
reason='ctrl-c (SIGINT)', due_to_sigint=True, force_sync=True
198+
reason=base_msg, due_to_sigint=True, force_sync=True
198199
)
199200
assert ret is None, ret
200-
sigint_received = True
201201
else:
202202
self.__logger.warning('{} again, ignoring...'.format(base_msg))
203203

204-
def _on_sigterm(signum):
205-
signame = signal.Signals(signum).name
206-
self.__logger.error(
207-
'user interrupted with ctrl-\\ ({}), terminating...'.format(signame))
208-
# TODO(wjwwood): try to terminate running subprocesses before exiting.
209-
self.__logger.error('using {} can result in orphaned processes'.format(signame))
210-
self.__logger.error('make sure no processes launched are still running')
211-
this_loop.call_soon(this_task.cancel)
212-
213204
with AsyncSafeSignalManager(this_loop) as manager:
214205
# Setup signal handlers
215-
manager.handle(signal.SIGINT, _on_sigint)
216-
manager.handle(signal.SIGTERM, _on_sigterm)
206+
manager.handle(signal.SIGINT, _on_signal)
207+
manager.handle(signal.SIGTERM, _on_signal)
217208
# Yield asyncio loop and current task.
218209
yield this_loop, this_task
219210
finally:

0 commit comments

Comments
 (0)