Skip to content

Commit 964250e

Browse files
committed
move logic out of async fn to avoid race, and fixups
Signed-off-by: William Woodall <[email protected]>
1 parent 2c4bb24 commit 964250e

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

launch/launch/actions/execute_local.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -578,17 +578,6 @@ async def __execute_process(self, context: LaunchContext) -> None:
578578
raise RuntimeError('process_event_args unexpectedly None')
579579

580580
cmd = process_event_args['cmd']
581-
if evaluate_condition_expression(context, self.__split_arguments):
582-
if self.__shell:
583-
self.__logger.debug("Ignoring 'split_arguments=True' because 'shell=True'.")
584-
else:
585-
self.__logger.debug("Splitting arguments because 'split_arguments=True'.")
586-
expanded_cmd = []
587-
for token in cmd:
588-
expanded_cmd.extend(shlex.split(token))
589-
cmd = expanded_cmd
590-
# Also update process_event_args so they reflect the splitting.
591-
process_event_args['cmd'] = cmd
592581
cwd = process_event_args['cwd']
593582
env = process_event_args['env']
594583
if self.__log_cmd:
@@ -759,6 +748,18 @@ def execute(self, context: LaunchContext) -> Optional[List[LaunchDescriptionEnti
759748
else:
760749
self.__stdout_logger, self.__stderr_logger = \
761750
launch.logging.get_output_loggers(name, self.__output)
751+
# Update the cmd to respect split_arguments option.
752+
if evaluate_condition_expression(context, self.__split_arguments):
753+
if self.__shell:
754+
self.__logger.debug("Ignoring 'split_arguments=True' because 'shell=True'.")
755+
else:
756+
self.__logger.debug("Splitting arguments because 'split_arguments=True'.")
757+
expanded_cmd = []
758+
assert self.__process_event_args is not None
759+
for token in self.__process_event_args['cmd']:
760+
expanded_cmd.extend(shlex.split(token))
761+
# Also update self.__process_event_args['cmd'] so it reflects the splitting.
762+
self.__process_event_args['cmd'] = expanded_cmd
762763
context.asyncio_loop.create_task(self.__execute_process(context))
763764
except Exception:
764765
for event_handler in event_handlers:

launch/launch/actions/execute_process.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,21 @@ def _append_arg():
262262
nonlocal arg
263263
result_args.append(arg)
264264
arg = []
265+
265266
for sub in parser.parse_substitution(cmd):
266267
if isinstance(sub, TextSubstitution):
267268
try:
268269
tokens = shlex.split(sub.text)
269-
except:
270+
except Exception:
270271
logger = launch.logging.get_logger(cls.__name__)
271272
logger.error(f"Failed to parse token '{sub.text}' of cmd '{cmd}'")
272273
raise
273-
if not tokens:
274-
# String with just spaces.
274+
if len(tokens) == 0:
275+
# String with just spaces produces an empty list.
275276
# Appending args allow splitting two substitutions
276-
# separated by a space.
277-
# e.g.: `$(subst1 asd) $(subst2 bsd)` will be two separate arguments.
277+
# separated by some amount of whitespace.
278+
# e.g.: `$(subst1 asd) $(subst2 bsd)` will be two separate arguments,
279+
# first `$(subst1 asd)`, then this case ` `, then the `$(subst2 bsd)`.
278280
_append_arg()
279281
continue
280282
if sub.text[0].isspace():

launch/test/launch/test_execute_process.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from launch.actions.register_event_handler import RegisterEventHandler
3030
from launch.actions.shutdown_action import Shutdown
3131
from launch.actions.timer_action import TimerAction
32-
from launch.conditions import evaluate_condition_expression
3332
from launch.event_handlers.on_process_start import OnProcessStart
3433
from launch.events.shutdown import Shutdown as ShutdownEvent
3534
from launch.substitutions.launch_configuration import LaunchConfiguration

0 commit comments

Comments
 (0)