ros2 / launch

Tools for launching multiple processes and for writing tests involving multiple processes.
Apache License 2.0
125 stars 139 forks source link

Fixup environment leakage for Command #740

Open haudren-woven opened 11 months ago

haudren-woven commented 11 months ago

The Command substitution was not using the context's environment to launch itself, and defaults to the parent process environment. This caused bugs, as described in #715, which I re-state below.

Fixing this is very simple, just pass the environment of the context to the subprocess spawned in Command.

How to reproduce? I couldn't quite come up with a very simple unit test yet, but the below procedure should work.

First a simple executable:

import os

print(os.environ.get("FOO", "There is no foo"))

Place it somewhere on your path, I named it print_env.py.

Then the following launch file:

import launch

def generate_launch_description():
    ld = launch.LaunchDescription()

    ld.add_action(
        launch.actions.ExecuteProcess(
            cmd=["print_env.py"], additional_env={"FOO": "BAR"}, output="screen"
        )
    )

    ld.add_action(launch.actions.ExecuteProcess(cmd=["print_env.py"], output="screen"))

    ld.add_action(
        launch.actions.ExecuteProcess(
            cmd=["print_env.py"],
            output="screen",
            additional_env={
                "FOO": launch.substitutions.Command(
                    [
                        "print_env.py",
                    ]
                )
            },
        )
    )

    return ld

This launches three "printenv" processes:

So I expect:

What I get: