Skip to content

Commit

Permalink
ExecuteProcess: unregister event handlers if rest of setup fails
Browse files Browse the repository at this point in the history
The event handlers need to be setup before the other lines, but are invalid if setup does not complete successfully.
  • Loading branch information
wjwwood committed Aug 30, 2018
1 parent b4969f4 commit f077ab6
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions launch/launch/actions/execute_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,25 +451,35 @@ def execute(self, context: LaunchContext) -> Optional[List['Action']]:
if self.__shutdown_received:
# If shutdown starts before execution can start, don't start execution.
return None
# TODO(wjwwood): unregister event handlers when that is possible
context.register_event_handler(EventHandler(
matcher=lambda event: is_a_subclass(event, ShutdownProcess),
entities=OpaqueFunction(function=self.__on_shutdown_process_event),
))
context.register_event_handler(EventHandler(
matcher=lambda event: is_a_subclass(event, SignalProcess),
entities=OpaqueFunction(function=self.__on_signal_process_event),
))
context.register_event_handler(EventHandler(
matcher=lambda event: is_a_subclass(event, ProcessStdin),
entities=OpaqueFunction(function=self.__on_process_stdin_event),
))
context.register_event_handler(OnShutdown(
on_shutdown=self.__on_shutdown,
))
self.__completed_future = create_future(context.asyncio_loop)
self.__expand_substitutions(context)
context.asyncio_loop.create_task(self.__execute_process(context))

event_handlers = [
EventHandler(
matcher=lambda event: is_a_subclass(event, ShutdownProcess),
entities=OpaqueFunction(function=self.__on_shutdown_process_event),
),
EventHandler(
matcher=lambda event: is_a_subclass(event, SignalProcess),
entities=OpaqueFunction(function=self.__on_signal_process_event),
),
EventHandler(
matcher=lambda event: is_a_subclass(event, ProcessStdin),
entities=OpaqueFunction(function=self.__on_process_stdin_event),
),
OnShutdown(
on_shutdown=self.__on_shutdown,
),
]
for event_handler in event_handlers:
context.register_event_handler(event_handler)

try:
self.__completed_future = create_future(context.asyncio_loop)
self.__expand_substitutions(context)
context.asyncio_loop.create_task(self.__execute_process(context))
except Exception:
for event_handler in event_handlers:
context.unregister_event_handler(event_handler)
raise
return None

def get_asyncio_future(self) -> Optional[asyncio.Future]:
Expand Down

0 comments on commit f077ab6

Please sign in to comment.