Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 21, 2025

This PR implements a simplified task routing mechanism for multi-threaded execution mode as described in the multithreaded MSBuild specification.

Overview

The implementation provides a foundation for routing tasks based on thread safety when using the existing /multithreaded command-line switch. Thread-safe tasks execute within thread nodes (in-process), while non-thread-safe tasks are routed to sidecar taskhost processes.

Key Components

ThreadSafeTaskRegistry

  • Maintains an immutable allowlist of tasks known to be thread-safe using FrozenSet<string> for optimal performance
  • Initially includes core MSBuild tasks: Message, Warning, Error, ItemGroup, PropertyGroup, CallTarget, MSBuild
  • Provides case-insensitive lookup with a minimal read-only API focused on the essential IsTaskThreadSafe() method

Task Routing Logic

  • Added ShouldTaskExecuteOutOfProc() method to TaskHost class
  • Routes tasks based on the following priority:
    1. If already out-of-process → always route out-of-process (preserves existing behavior)
    2. If MultiThreaded = false → use default behavior (no change to existing builds)
    3. If MultiThreaded = true → thread-safe tasks run in-process, others go to sidecar taskhost

Integration with Existing Infrastructure

  • Leverages the existing BuildParameters.MultiThreaded property
  • Works with the existing /multithreaded (or /mt) command-line switch
  • No changes to the public API or breaking changes to existing functionality

Usage

Enable task routing using the existing experimental multi-threaded switch:

# Enable multi-threaded execution with task routing
msbuild MyProject.proj /multithreaded

# Short form
msbuild MyProject.proj /mt

Testing

The implementation includes focused unit tests covering:

  • ThreadSafeTaskRegistry core functionality (3 tests)
  • Task routing decision logic (4 tests)
  • Edge cases like null/empty task names and existing out-of-process scenarios

Manual testing confirms that:

  • Normal builds are unaffected (backwards compatibility preserved)
  • Multi-threaded builds correctly route thread-safe tasks to thread nodes
  • Non-thread-safe tasks are properly isolated in sidecar processes

Future Work

This provides the foundation for the multi-threaded execution mode. As tasks are migrated to implement the new thread-safe interface (per #11828), they can be added to the allowlist to benefit from in-process execution.

Fixes #12196.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo.

@Copilot Copilot AI changed the title [WIP] Implement task routing for multi-threaded execution Implement task routing for multi-threaded execution mode Aug 21, 2025
@Copilot Copilot AI requested a review from JanProvaznik August 21, 2025 13:00
Copilot finished work on behalf of JanProvaznik August 21, 2025 13:00
Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

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

@copilot keep it simple

Copy link
Contributor Author

Copilot AI commented Aug 21, 2025

@copilot keep it simple

Simplified the implementation by using FrozenSet and removing all dynamic modification methods and their corresponding tests. The ThreadSafeTaskRegistry now has a minimal, focused API with just the essential IsTaskThreadSafe method. Commit 5faddcc

@Copilot Copilot AI changed the title Implement task routing for multi-threaded execution mode Implement simplified task routing for multi-threaded execution mode Aug 21, 2025
@Copilot Copilot AI requested a review from JanProvaznik August 21, 2025 16:21
Copilot finished work on behalf of JanProvaznik August 21, 2025 16:21
Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

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

adjust the testing strategy @copilot

{
/// <summary>
/// Registry that maintains an allowlist of tasks that are known to be thread-safe
/// and can be executed within thread nodes in multi-threaded execution mode.
Copy link
Member

Choose a reason for hiding this comment

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

rewrite the documentation of this file noting it is a temporary solution before we adopt a Thread safe task interface

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the documentation to clarify this is a temporary solution before adopting a thread-safe task interface. The comments now explain that this is an interim allowlist approach that will be replaced with interface-based detection once a formal thread-safe task interface is established. Commit 8811159

/// Verify that non-thread-safe tasks execute out-of-process when multi-threaded execution is enabled.
/// </summary>
[Fact]
public void NonThreadSafeTasksExecuteOutOfProcessWhenMultiThreadedExecutionEnabled()
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to see tests that prove a safe task (Message) actually runs in another process. And that an unsafe one (Copy) doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enhanced the testing strategy with integration tests that actually execute Message and Copy tasks using BuildManager with MultiThreaded=true. The new tests verify:

  • Message task executes in-process (thread-safe routing)
  • Copy task executes out-of-process (non-thread-safe routing)
  • Both tasks complete successfully under multithreaded execution

Also added comprehensive parameterized tests covering all routing scenarios. Commit 8811159

…egy with integration tests

Co-authored-by: JanProvaznik <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 22, 2025

adjust the testing strategy @copilot

Adjusted the testing strategy to include:

  1. Focused unit tests for specific task routing scenarios (Message vs Copy)
  2. Integration tests that actually execute tasks using BuildManager with multithreaded parameters
  3. Comprehensive parameterized tests covering all routing decision paths
  4. Tests that verify both routing logic and actual task execution success

The new approach provides better coverage of the actual behavior while maintaining focused, maintainable tests. Commit 8811159

@Copilot Copilot AI requested a review from JanProvaznik August 22, 2025 10:12
Copilot finished work on behalf of JanProvaznik August 22, 2025 10:12
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.

Implement task routing for multi-threaded execution
2 participants