dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.17k stars 1.34k forks source link

[Bug]: OOM Exception due to infinite loop in FileMatcher #10187

Open sebastienros opened 1 month ago

sebastienros commented 1 month ago

Issue Description

Got consistent OOM exceptions while building aspnet locally. Weird thing is that the MSBUILD.exe 32bits was the process hanging.

MSBUILD : error :     System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemor
yException' was thrown. [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at System.String.CtorCharCount(Char c, Int32 count) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Bu
ild.proj]
MSBUILD : error :    at Microsoft.IO.StringExtensions.Create[TState](Int32 length, TState state, SpanAction`2 action) [D:\.nuget\packages\microsoft.
dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Path.JoinInternal(ReadOnlySpan`1 first, ReadOnlySpan`1 second) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\
9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Path.Join(ReadOnlySpan`1 path1, ReadOnlySpan`1 path2) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-bet
a.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Path.Join(ReadOnlySpan`1 path1, ReadOnlySpan`1 path2, ReadOnlySpan`1 path3) [D:\.nuget\packages\microsoft.dotne
t.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Enumeration.FileSystemEntry.ToSpecifiedFullPath() [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24
266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Enumeration.FileSystemEnumerableFactory.<>c.<UserFiles>b__3_0(FileSystemEntry& entry) [D:\.nuget\packages\micro
soft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Enumeration.FileSystemEnumerable`1.DelegateEnumerator.TransformEntry(FileSystemEntry& entry) [D:\.nuget\package
s\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.IO.Enumeration.FileSystemEnumerator`1.MoveNext() [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.
1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<RemoveInitialDotSlash>d__35.MoveNext() [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9
.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta
.24266.1\tools\Build.proj]
MSBUILD : error :    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.2426
6.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetAccessibleFiles(IFileSystem fileSystem, String path, String filespec, String projectDi
rectory, Boolean stripProjectDirectory) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetAccessibleFileSystemEntries(IFileSystem fileSystem, FileSystemEntity entityType, Strin
g path, String pattern, String projectDirectory, Boolean stripProjectDirectory) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\t
ools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass18_0.<.ctor>b__0(FileSystemEntity entityType, String path, String patter
n, String projectDirectory, Boolean stripProjectDirectory) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass19_1.<.ctor>b__1(String s) [D:\.nuget\packages\microsoft.dotnet.arcade.s
dk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) [D:\.nuget\packages\microsoft.d
otnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass19_0.<.ctor>b__0(FileSystemEntity type, String path, String pattern, Str
ing directory, Boolean stripProjectDirectory) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetFilesForStep(RecursiveStepResult stepResult, RecursionState recursionState, String pro
jectDirectory, Boolean stripProjectDirectory) [D:\.nuget\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetFilesRecursive(ConcurrentStack`1 listOfFiles, RecursionState recursionState, String pr
ojectDirectory, Boolean stripProjectDirectory, IList`1 searchesToExclude, Dictionary`2 searchesToExcludeInSubdirs, TaskOptions taskOptions) [D:\.nug
et\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass41_0.<GetFilesRecursive>b__0(String subdir) [D:\.nuget\packages\microsof
t.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetFilesRecursive(ConcurrentStack`1 listOfFiles, RecursionState recursionState, String pr
ojectDirectory, Boolean stripProjectDirectory, IList`1 searchesToExclude, Dictionary`2 searchesToExcludeInSubdirs, TaskOptions taskOptions) [D:\.nug
et\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass41_0.<GetFilesRecursive>b__0(String subdir) [D:\.nuget\packages\microsof
t.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetFilesRecursive(ConcurrentStack`1 listOfFiles, RecursionState recursionState, String pr
ojectDirectory, Boolean stripProjectDirectory, IList`1 searchesToExclude, Dictionary`2 searchesToExcludeInSubdirs, TaskOptions taskOptions) [D:\.nug
et\packages\microsoft.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.<>c__DisplayClass41_0.<GetFilesRecursive>b__0(String subdir) [D:\.nuget\packages\microsof
t.dotnet.arcade.sdk\9.0.0-beta.24266.1\tools\Build.proj]
MSBUILD : error :    at Microsoft.Build.Shared.FileMatcher.GetFilesRecursive(ConcurrentStack`1 listOfFiles, RecursionState recursionState, String pr
o

... more calls to GetFilesRecursive

Steps to Reproduce

I can't anymore, I cleaned my repos (git clean -xdff) and then the issue went away, so definitely due to some specific pattern in my folder structure.

Expected Behavior

No exception

Actual Behavior

Exception

Analysis

Maybe knowing that there can be a recursivity issue will let you see it without repro. Sorry I don't have one to share.

Versions & Configurations

No response

sebastienros commented 1 month ago

Got it to repro today. And it was using C:\Program Files\Microsoft Visual Studio\2022\IntPreview\MSBuild\Current\Bin\MSBuild.exe.

I was in a standard powershell terminal, with the developer one it finds \amd64\msbuild.exe, but the memory keeps growing, I killed it at 16GB.

So there is an infinite loop, and I listed all symlinks, I found this one to be a potential issue:

    Directory: D:\aspnetcore\src\Components\test\E2ETest\node_modules

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
l----           5/30/2024  4:48 PM                aspnetcore -> D:\aspnetcore\

Hoping that it gives you a way to detect these cases and exit instead of looping indefinitely.

AR-May commented 4 weeks ago

Team triage: That is a bug indeed, and a known one: we have a PR #7685 fixing this for the dotnet core version, but not for the framework version of MSBuild. We should fix it for the framework version too.