Skip to content

Initialize TaskEnvironment in out-of-proc task hosts - #15183

Merged
JanProvaznik merged 2 commits into
dotnet:mainfrom
JanProvaznik:janprovaznik-task-environment-issue
Oct 2, 2026
Merged

JanProvaznik merged 2 commits into
dotnet:mainfrom
JanProvaznik:janprovaznik-task-environment-issue

Conversation

@JanProvaznik

Copy link
Copy Markdown
Member

Fixes #13714

Context

The out-of-proc task host passes TaskEnvironment.Fallback to environment-taking constructors but never assigns IMultiThreadableTask.TaskEnvironment afterward. Tasks with only a parameterless constructor and no default initializer are left with a null environment, unlike the in-process execution path. Built-in fallback initializers and constructor injection mask this gap.

Changes Made

  • Assign TaskEnvironment.Fallback before task parameter binding and execution.
  • For AppDomain-isolated tasks, perform the assignment inside the task's AppDomain so the nonserializable TaskEnvironment does not cross the boundary.
  • Add regression coverage and update the task-host and thread-safe task specifications.

Testing

  • Confirmed the parameterless-task regression fails before the fix.
  • Built the command-line unit-test project for both target frameworks with no warnings or errors.
  • Focused wrapper tests pass: 3/3 on net11.0, 4/4 on net472, with no skips. Coverage includes parameter setters, execution, constructor-injected environment identity, and actual AppDomain isolation.

Notes

This restores the existing property-injection contract without changing task routing, public APIs, protocols, or diagnostics. Constructor injection remains unchanged. Tests exercise the real task wrapper, not a separate-process launch.

Assign the fallback environment before parameter binding and execution, including inside isolated AppDomains. Cover property-only and constructor-injected tasks and update the task-host specifications.

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The targeted fix preserves existing routing and constructor behavior while providing regression coverage for all affected execution paths.

Review effort: Balanced
Findings: None

What changed in this PR

Restores TaskEnvironment property injection for out-of-process task hosts, including AppDomain-isolated tasks.

Changes:

  • Assigns TaskEnvironment.Fallback before parameter binding and execution.
  • Performs AppDomain assignments without marshaling TaskEnvironment.
  • Adds regression tests and updates threading specifications.
File Description
src/​MSBuild/​OutOfProcTaskAppDomainWrapperBase.cs Implements out-of-process environment injection.
src/​MSBuild.UnitTests/​OutOfProcTaskAppDomainWrapper_Tests.cs Tests parameterless, constructor-injected, and AppDomain tasks.
documentation/​specs/​multithreading/​thread-safe-tasks.md Documents the restored injection contract.
documentation/​specs/​multithreading/​taskhost-threading.md Updates TaskHost execution sequencing.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Preserve upstream task-parameter conversion and its regression test alongside TaskEnvironment initialization and AppDomain coverage.

Co-authored-by: Copilot App <[email protected]>
@AR-May

AR-May commented Oct 2, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@JanProvaznik
JanProvaznik merged commit 59c8d1d into dotnet:main Oct 2, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task hosts should set TaskEnvironment instead of relying on task defaults

3 participants