Repeated failed LoadFromAssemblyPath on a native DLL ends in 'Stack overflow.' and a hung process instead of BadImageFormatException
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 31/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
Start with the AssemblyLoadContext.Default.LoadFromAssemblyPath reproduction in Program.cs and NativeLoadRepro.csproj, then investigate the Windows native loading path and its handling of repeated BadImageFormatException failures. Reproduce on the reported .NET 10.0.13 Windows x64 setup and trace the native stack around the overflow and lock. Done means repeated failed loads return BadImageFormatException without overflowing or hanging.
由索引模型根据 Issue 内容生成。
描述
Written by Claude Code, an AI agent.
Description
Calling AssemblyLoadContext.Default.LoadFromAssemblyPath on the same native (non-managed) DLL again and again, on one thread, throws BadImageFormatException each time at first. After about 1,000 calls for covrun64.dll (the code coverage's native profiler DLL, from Microsoft.Testing.Extensions.CodeCoverage), the process prints Stack overflow. and then hangs: it uses no CPU, and does not exit.
A library that scans its output folder for extensions does exactly this: it tries to load every *.dll, and a test process that starts many hosts repeats the scan. Here the test process hung after about 300 tests. The library's part is reported separately: https://github.com/JasperFx/jasperfx/issues/1143.
Reproduction Steps
NativeLoadRepro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- Only for its native DLLs, which it copies to runtimes/win-*/native. -->
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.12.0" />
</ItemGroup>
</Project>
Program.cs:
using System.Runtime.Loader;
var file = Path.Combine(AppContext.BaseDirectory, "runtimes", "win-x64", "native", "covrun64.dll");
for (var i = 1; i <= 100_000; i++)
{
try
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(file);
}
catch (BadImageFormatException)
{
}
if (i % 100 == 0)
{
Console.WriteLine($"{i} failed loads");
}
}
Console.WriteLine("done");
dotnet run -c Release on Windows x64.
Expected behavior
Every call throws BadImageFormatException, and the loop prints done.
Actual behavior
100 failed loads
...
900 failed loads
1000 failed loads
Stack overflow.
Then the process hangs until it is killed. Run today: a 120-second limit killed it.
What else was measured on the same machine:
covrun64.dllalone: the overflow came after 1,081 failed loads in one run, and between 1,000 and 1,100 in the run above.- All 24 native DLLs of the package's
runtimesfolder in turn: after about 8,650 failed loads. - The count adds up per process, not per thread: spreading the loads over several threads gives the same total.
msdia140.dllfrom the same folder survived 71,928 failed loads, and loads by name only (LoadFromAssemblyName) of the same files never overflowed.- A dump of the hung test process (
dotnet-dump, managed frames only) showed the loading thread inside the native part ofLoadFromAssemblyPathonruntimes\win-arm64\native\covrun64.dll, holding a lock that other threads waited on inMonitor.Enter. No native debugger was available, so the native stack was not read.
Regression?
Not known. Only .NET 10.0.13 was tried.
Known Workarounds
Don't load native DLLs by path: skip the runtimes and native folders when scanning.
Configuration
- .NET runtime 10.0.13, SDK 10.0.402
- Windows 11 Pro 10.0.26300, x64
- Microsoft.Testing.Extensions.CodeCoverage 18.12.0
- Windows Defender real-time protection is on. No other endpoint security product is installed. microsoft/codecoverage#212 reported a similar crash on the same file and was closed because the reporter traced it to an endpoint security product; on this machine the only such product is Defender, and this report cannot rule out its part.
Other information
The stack overflow suggests something grows per failed load of this file (perhaps a list of failed images, or a chain of handlers) and is walked recursively. That is a guess, not measured.
- 主要语言
- C#
- 星标
- 18.4k
- 派生
- 5.8k
- 平均合并
- 2 天 14 小时
- 30 天内合并 PR
- 585
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/runtime 的其他 Issue
-
area-Host untriaged
难度 2/5 1-3 小时 新手友好度 68/100
dotnet/runtime#137187 · 3 条评论 ·
维护者通常 1 天内回复
-
ImmutableSortedSet<T>.Builder: enumerating through IEnumerable<T> doesn't throw on modification and can return wrong items可能已有人在做 @aw0lid 今天认领。 未关闭area-System.Collections
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#137167 · 1 条评论 ·
维护者通常 1 天内回复
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by design可能已有人在做 @alaricd 于 4 天前认领。 未关闭area-System.Transactions untriaged
难度 2/5 1-3 小时 新手友好度 72/100
dotnet/runtime#137040 · 1 条评论 ·
维护者通常 1 天内回复
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the file可能已有人在做 @makazeu 于 4 天前认领。 未关闭area-Extensions-FileSystem
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#136989 · 1 条评论 ·
维护者通常 1 天内回复
-
Configuration binder source generator emits unqualified List<T> when binding arrays without implicit usings可能已有人在做 @makazeu 于 4 天前认领。 未关闭area-Extensions-Configuration bug source-generator
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#136972 · 1 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
Issue-Bug Needs-Triage
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
维护者通常 1 天内回复
-
area/access-management Frontend onboarding candidate :handshake:
难度 2/5 1-3 小时 新手友好度 65/100
Altinn/altinn-auth#4941 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
aws/aws-sdk-net#4586 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复