Multithreaded task routing#12617
Merged
JanProvaznik merged 14 commits intodotnet:mainfrom Oct 21, 2025
Merged
Conversation
AR-May
added a commit
that referenced
this pull request
Oct 14, 2025
Related to #11828 Changes: This PR introduces the core APIs that enable thread-safe task execution in MSBuild's multithreaded execution model. These APIs provide safe alternatives to global process state operations, allowing `IMultithreadable` tasks to run concurrently within a single MSBuild process. Node: Adding multithreaded APIs in order to unblock #12617 Extracted APIs from #12608 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Chet Husk <chusk3@gmail.com>
fb6944a to
18fe1c5
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements automatic task routing for multi-threaded MSBuild builds. Tasks that declare thread-safety (via IMultiThreadableTask interface or [MSBuildMultiThreadableTask] attribute) run in-process, while legacy tasks without these indicators are isolated in TaskHost processes for safety. Single-threaded builds remain unchanged.
Key Changes
- Routing logic centralizes task safety evaluation with reflection-based caching
- Integration point in
AssemblyTaskFactoryapplies routing only in multi-threaded mode - Comprehensive test coverage validates routing decisions across all scenarios
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
Microsoft.Build.csproj |
Adds TaskRoutingDecision.cs to project compilation |
AssemblyTaskFactory.cs |
Integrates routing decision after architecture checks in CreateTaskInstance |
TaskRoutingDecision.cs |
Implements routing logic with interface/attribute detection and type-based caching |
TaskRouting_IntegrationTests.cs |
Adds 7 integration tests covering all routing scenarios and a custom attribute definition for testing |
AR-May
reviewed
Oct 16, 2025
src/Build/BackEnd/Components/RequestBuilder/TaskRoutingDecision.cs
Outdated
Show resolved
Hide resolved
src/Build/BackEnd/Components/RequestBuilder/TaskRoutingDecision.cs
Outdated
Show resolved
Hide resolved
AR-May
approved these changes
Oct 16, 2025
Member
AR-May
left a comment
There was a problem hiding this comment.
Looks good to me! I would like though either @rainersigwald or @baronfel approve of our task routing strategy here.
This was referenced Oct 21, 2025
Contributor
|
I have feedback to consider in the future.
|
Member
Author
|
This was referenced Feb 23, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task Routing for Multi-Threaded MSBuild
Summary
Implements automatic task routing for multi-threaded MSBuild. Thread-safe tasks (via
IMultiThreadableTaskinterface or[MSBuildMultiThreadableTask]attribute) run in-process; others route to isolated TaskHost processes. Traditional builds unchanged.Routing Logic
Simplified Semantics: The
[MSBuildMultiThreadableTask]attribute is the sole determinant for multi-threaded task routing. The attribute is non-inheritable (Inherited = false), meaning each task class must explicitly declare its thread-safety capability.Tasks may also implement
IMultiThreadableTaskto gain access toTaskEnvironmentAPIs, but the interface alone does not affect routing - only the attribute matters.Explicit
TaskHostFactoryspecification overrides all routing decisions.Thread-Safety Declaration
Attribute-Based (Required)
TaskEnvironmentAPI assistanceInterface-Based (Optional Enhancement)
Requires MSBuild 10.0+. The attribute controls routing; the interface provides
TaskEnvironmentAPI access.Nonenlightened Tasks (No Changes Required)
Routed to TaskHost in multi-threaded mode. No changes required.
Implementation
Core Components
TaskRouter.cs - Simplified routing logic with caching
NeedsTaskHostInMultiThreadedMode()- Returns true if task lacksMSBuildMultiThreadableTaskAttributeHasMultiThreadableTaskAttribute()- Checks for attribute using name-based matching (cached per type)inherit: falsesince attribute is non-inheritableAssemblyTaskFactory.cs - Integration point
Attribute Detection
Name-based matching allows task assemblies to define their own attribute copy without depending on MSBuild internals. Results are cached for performance.
Files Changed
New
TaskRouter.cs- Simplified routing logic with cachingModified
MSBuildMultiThreadableTaskAttribute.cs- Made non-inheritable (Inherited = false)AssemblyTaskFactory.cs- Integrated routing decision, added XML documentationTaskHostTask.cs- Fixed custom event parameter orderingTaskExecutionHost.cs- Pass scheduledNodeId throughTaskRouter_IntegrationTests.cs- Updated integration tests for simplified routing logicTest Utilities
TaskRouterTestHelper- Semantic log assertions to reduce test fragilityTesting
Integration Tests (7 scenarios)
Compatibility
No breaking changes. Nonenlightened tasks automatically isolated in TaskHost. Single-threaded mode unchanged.