Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

Replaying a binary log with terminal logger fails when the replay uses fewer nodes (/m:1) than the original build. The logger initializes its _nodes array based on the replay's node count, but events in the binlog reference NodeIds from the original build, causing an IndexOutOfRangeException.

Changes

  • Added EnsureNodeCapacity() to dynamically resize _nodes array when encountering NodeIds beyond current capacity
  • Updated all _nodes array accesses (in UpdateNodeStatus, ProjectStarted, MessageRaised) to ensure capacity before indexing
  • Added test case validating binlog replay with mismatched node counts
private void EnsureNodeCapacity(int nodeIndex)
{
    if (nodeIndex >= _nodes.Length)
    {
        int newSize = Math.Max(nodeIndex + 1, _nodes.Length * 2);
        Array.Resize(ref _nodes, newSize);
    }
}

private void UpdateNodeStatus(BuildEventContext buildEventContext, TerminalNodeStatus? nodeStatus)
{
    int nodeIndex = NodeIndexForContext(buildEventContext);
    EnsureNodeCapacity(nodeIndex);  // Ensure array can accommodate this index
    _nodes[nodeIndex] = nodeStatus;
}

Fixes #10596

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • rcxvsblobprodcus328.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/msbuild/msbuild/artifacts/bin/bootstrap/core/dotnet dotnet test src/Build.UnitTests/Microsoft.Build.Engine.UnitTests.csproj --filter FullyQualifiedName~ReplayBinaryLogWithFewerNodesThanOriginalBuild --no-build -v n (dns block)
    • Triggering command: /home/REDACTED/work/msbuild/msbuild/artifacts/bin/bootstrap/core/dotnet dotnet test src/Build.UnitTests/Microsoft.Build.Engine.UnitTests.csproj --filter FullyQualifiedName~TerminalLogger --no-build -v n (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Replaying binary log with terminal logger on sometimes may fail.</issue_title>
<issue_description>### Issue Description

Replaying a binary log with terminal logger fails when the number of nodes specified for replay command is less than the number of nodes originally used during the build.

Steps to Reproduce

  1. Collect a binary log for a multi-project solution build.
  2. Attempt to replay it with /m:1

Expected Behavior

Replay succeeds.

Actual Behavior

Getting error "There was an exception while reading the log file: Index was outside the bounds of the array." from the terminal logger.

Stack trace:

   at Microsoft.Build.Logging.TerminalLogger.UpdateNodeStatus(BuildEventContext buildEventContext, TerminalNodeStatus nodeStatus) in C:\Users\alinama\work\msbuild\msbuild\src\Build\Logging\TerminalLogger\TerminalLogger.cs:line 990
   at Microsoft.Build.Logging.TerminalLogger.TargetStarted(Object sender, TargetStartedEventArgs e) in C:\Users\alinama\work\msbuild\msbuild\src\Build\Logging\TerminalLogger\TerminalLogger.cs:line 977
   at Microsoft.Build.Logging.EventArgsDispatcher.Dispatch(BuildEventArgs buildEvent) in C:\Users\alinama\work\msbuild\msbuild\src\Build\Logging\BinaryLogger\EventArgsDispatcher.cs:line 123
   at Microsoft.Build.Logging.BinaryLogReplayEventSource.Replay(BuildEventArgsReader reader, CancellationToken cancellationToken) in C:\Users\alinama\work\msbuild\msbuild\src\Build\Logging\BinaryLogger\BinaryLogReplayEventSource.cs:line 264
   at Microsoft.Build.Logging.BinaryLogReplayEventSource.Replay(String sourceFilePath, CancellationToken cancellationToken) in C:\Users\alinama\work\msbuild\msbuild\src\Build\Logging\BinaryLogger\BinaryLogReplayEventSource.cs:line 201
   at Microsoft.Build.CommandLine.MSBuildApp.ReplayBinaryLog(String binaryLogFilePath, ILogger[] loggers, IEnumerable`1 distributedLoggerRecords, Int32 cpuCount, Boolean isBuildCheckEnabled) in C:\Users\alinama\work\msbuild\msbuild\src\MSBuild\XMake.cs:line 4602

Analysis

This happens because of how terminal logger determines number of nodes to display. During the replay mode it uses currently specified number of nodes, instead of using the number of nodes that was used during the binary log collection.

Workaround: use /m:big_enough_number during replaying.

Versions & Configurations

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

@dotnet-policy-service
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 AI changed the title [WIP] Fix binary log replay failure with terminal logger Fix TerminalLogger IndexOutOfRangeException when replaying binlog with fewer nodes Nov 21, 2025
Copilot AI requested a review from YuliiaKovalova November 21, 2025 18:58
Copilot finished work on behalf of YuliiaKovalova November 21, 2025 18:58
@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review December 1, 2025 12:28
Copilot finished reviewing on behalf of YuliiaKovalova December 1, 2025 12:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an IndexOutOfRangeException that occurs when replaying a binary log with the TerminalLogger using fewer nodes than the original build. The issue arose because the logger initializes its _nodes array based on the replay's node count, but events in the binlog reference NodeIds from the original build with potentially higher values.

Key changes:

  • Added dynamic array resizing via EnsureNodeCapacity() method
  • Protected all _nodes array accesses in event handlers to ensure capacity before indexing
  • Added test case for binlog replay with mismatched node counts

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Build/Logging/TerminalLogger/TerminalLogger.cs Added EnsureNodeCapacity() method with exponential growth strategy and updated all three array access points (ProjectStarted, UpdateNodeStatus, MessageRaised) to call it before indexing
src/Build.UnitTests/TerminalLogger_Tests.cs Added regression test that builds with /m:4 and replays with nodeCount: 1 to verify the fix works

Should.NotThrow(() => replayEventSource.Replay(binlogPath));

terminalLogger.Shutdown();
}
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

Missing closing brace for the test method. The using block closes at line 1060, but the method itself needs a closing brace before the next test method starts at line 1062.

Suggested change
}
}
}

Copilot uses AI. Check for mistakes.
Comment on lines +1064 to +1065
int newSize = Math.Max(nodeIndex + 1, _nodes.Length * 2);
Array.Resize(ref _nodes, newSize);
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

Potential race condition: Array.Resize is not thread-safe and can cause issues when called concurrently with reads from the refresher thread. The comment at lines 113-115 states "reads and writes to locations in an array is atomic, so locking is not required," but this assumption breaks down with Array.Resize.

Array.Resize allocates a new array and copies elements, which is not atomic. The refresher thread calls DisplayNodes() under _lock at line 1441, which reads the entire _nodes array when constructing TerminalNodesFrame (TerminalNodesFrame.cs line 34). Meanwhile, event handlers like ProjectStarted, TargetStarted, and MessageRaised can call EnsureNodeCapacity without holding the lock, potentially causing a race condition.

Consider protecting EnsureNodeCapacity calls with the same _lock used in the refresher thread, or use a thread-safe alternative like Interlocked.CompareExchange to swap array references atomically.

Suggested change
int newSize = Math.Max(nodeIndex + 1, _nodes.Length * 2);
Array.Resize(ref _nodes, newSize);
lock (_lock)
{
if (nodeIndex >= _nodes.Length)
{
int newSize = Math.Max(nodeIndex + 1, _nodes.Length * 2);
Array.Resize(ref _nodes, newSize);
}
}

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

valid concern 👍

Comment on lines +1064 to +1065
int newSize = Math.Max(nodeIndex + 1, _nodes.Length * 2);
Array.Resize(ref _nodes, newSize);
Copy link
Member

Choose a reason for hiding this comment

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

valid concern 👍

await Verify(_outputWriter.ToString(), _settings).UniqueForOSPlatform().UseParameters(runOnCentralNode);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

syntax errors

}

[Fact]
public void ReplayBinaryLogWithFewerNodesThanOriginalBuild()
Copy link
Member

Choose a reason for hiding this comment

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

the test does not make sense, the build of this project will run on one node even though /m:4 is specified so no error will be produced when replayed with lower /m

{
_restoreContext = c;
int nodeIndex = NodeIndexForContext(e.BuildEventContext);
EnsureNodeCapacity(nodeIndex);
Copy link
Member

Choose a reason for hiding this comment

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

seems strange to ensure node capacity in the common path and it not being something specific for replay mode

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.

Replaying binary log with terminal logger on sometimes may fail. [Bug]: Specifying -graphBuild twice causes misleading error MSB1057

3 participants