Skip to content

MultiThreadedTaskEnvironmentDriver.GetProcessStartInfo() leaks inherited process env vars to child processes #13770

Closed

Description

@OvesN

Problem

MultiThreadedTaskEnvironmentDriver.GetProcessStartInfo() (src/Framework/MultiThreadedTaskEnvironmentDriver.cs:112-126) does not
clear the inherited process environment before applying the virtualized _environmentVariables. The resulting
ProcessStartInfo.EnvironmentVariables is lazily pre-populated with a snapshot of the MSBuild host process's env block, and the
loop only adds/overrides — never removes — entries.

As a result, any variable that was removed from the MT TaskEnvironment (via SetEnvironmentVariable(name, null) or
SetEnvironment(...) replacing the dictionary) still leaks into spawned child processes if it existed in the host MSBuild process.
This breaks env-var isolation between concurrently building projects under multithreaded mode and prevents tasks from scrubbing
inherited variables before spawning children.

Expected

The child process's environment should be exactly what the MT TaskEnvironment exposes — nothing more, nothing less.

Actual

Child inherits MSBuild host env on top of the virtualized env.

Likely fix

Clear startInfo.Environment before copying _environmentVariables into it.

Acceptance criteria — tests to add

Add tests under src/Framework.UnitTests/ (or appropriate MT driver test location) reproducing the leak:

  1. Removed variable does not leak

    • Set a process env var LEAK_TEST=yes (via TestEnvironment.SetEnvironmentVariable)
    • Construct a MultiThreadedTaskEnvironmentDriver (inherits host env)
    • Call SetEnvironmentVariable("LEAK_TEST", null)
    • Spawn cmd /c set LEAK_TEST (or sh -c 'echo $LEAK_TEST' on Unix) using the returned ProcessStartInfo
    • Assert child exits non-zero / output does not contain LEAK_TEST
  2. SetEnvironment produces exactly the supplied env

    • Set LEAK_TEST=yes in process env
    • Construct driver, call SetEnvironment(new Dictionary<string,string>{ ["OTHER"] = "x" })
    • Spawn child that prints its env; assert LEAK_TEST is absent and OTHER=x is present
  3. Concurrent drivers have independent envs

    • Driver A: SetEnvironmentVariable("K", "A")
    • Driver B: SetEnvironmentVariable("K", "B")
    • Spawn one child from each in parallel; assert each sees its own value for K

All three tests should fail on main and pass after the fix.

Scope

Pre-existing bug in MT infrastructure. Not caused by any in-flight task migration. Discovered during review of the Exec task
migration to TaskEnvironment API.

Activity

  1. self-assigned this
    on May 15, 2026
  2. OvesN commented on May 15, 2026

    @OvesN
    ContributorAuthor

    This is desired behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions