Repository navigation
Description
Description
Filed at the request of @adamsitnik in #137501 (the side effect described there). I know System.Web is .NET Framework and does not live in this repository; please route it to its owners.
The static initialization of System.Web.HttpRuntime (.NET Framework 4.8.1) calls the native webengine4!PerfCounterInitialize. As far as I can tell from the public symbols, that goes CPerfCounterServer::Initialize → CPerfCounterServer::CleanupNames, which walks \\.\pipe\* with FindFirstFileExW / FindNextFileW, so its cost depends on the number of named pipes that exist on the whole machine. In the measurements below, doubling the number of pipes from 10,000 to 20,000 raised the wall time of the whole PowerShell process from 1.6 s to 4.3 s.
This did not matter much while a machine had a few hundred named pipes. It matters since .NET 11:
- Since Process: use overlapped I/O for parent end of stdout/stderr pipes on Windows #125643 the parent end of a redirected child-process stream on Windows is a named pipe
\\.\pipe\LOCAL\dotnet_<guid>opened for overlapped I/O, andSafeFileHandle.CreateAnonymousPipe(..., asyncRead: true)creates the same kind of pipe. Every such pipe is an entry in the machine-wide pipe namespace (a pipe created withasyncRead: falseis not). - A handle leak in one .NET 11 process (for example .NET 11 regression on Windows: redirected Process.StandardOutput/StandardError leak one named pipe handle each after an async read unless the reader is disposed #137501) therefore slows down an unrelated component in every other process on the machine.
The component that made it visible for us is Windows PowerShell 5.1: ConvertFrom-Json / ConvertTo-Json use JavaScriptSerializer from System.Web.Extensions, so the first call in a powershell.exe process that triggers the System.Web.HttpRuntime initialization (measured: ConvertFrom-Json '{"a":1}' and ConvertTo-Json) pays for it. I only measured PowerShell; other .NET Framework processes that initialize System.Web.HttpRuntime outside ASP.NET should be affected in the same way.
Reproduction
.NET 11 console app. It holds N idle pipes and times a fresh powershell.exe with and without a JSON call:
using System.Diagnostics;
using Microsoft.Win32.SafeHandles;
// args: pipe counts in ascending order, e.g. 0 5000 10000 20000 (first arg "sync" = asyncRead: false as a control)
bool asyncRead = !(args.Length > 0 && args[0] == "sync");
int[] levels = args.Where(a => int.TryParse(a, out _)).Select(int.Parse).DefaultIfEmpty(0).ToArray();
const string Json = "$null = '{\\\"a\\\":1}' | ConvertFrom-Json";
const string Control = "$null = 1";
var pipes = new List<SafeFileHandle>();
try
{
foreach (int level in levels)
{
while (pipes.Count / 2 < level)
{
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle read, out SafeFileHandle write, asyncRead: asyncRead);
pipes.Add(read);
pipes.Add(write);
}
Console.WriteLine($"pipes held {pipes.Count / 2,6} | named pipes on machine {Directory.GetFiles(@"\\.\pipe\").Length,6} | " +
$"with ConvertFrom-Json {Median(Json),6} ms | without {Median(Control),5} ms");
}
}
finally
{
foreach (SafeFileHandle handle in pipes)
handle.Dispose();
}
static long Median(string command)
{
long[] times = new long[3];
for (int i = 0; i < times.Length; i++)
{
var stopwatch = Stopwatch.StartNew();
using var process = Process.Start(new ProcessStartInfo("powershell.exe", $"-NoProfile -NonInteractive -Command \"{command}\"")
{
UseShellExecute = false,
CreateNoWindow = true,
})!;
process.WaitForExit();
times[i] = stopwatch.ElapsedMilliseconds;
}
Array.Sort(times);
return times[1];
}Actual behavior
Windows 11 x64 (10.0.26200), .NET 11.0.0, .NET Framework 4.8.1 (webengine4.dll / System.Web.dll 4.8.9344.0), Windows PowerShell 5.1.26100.9711. Wall time of the whole powershell.exe process, median of 3 (other work was running on the machine, so the values below half a second are noisy):
idle pipes held (asyncRead: true) |
named pipes on the machine | powershell.exe with one ConvertFrom-Json |
same without JSON |
|---|---|---|---|
| 0 | 454 | 263 ms | 187 ms |
| 2,500 | 2,943 | 454 ms | 189 ms |
| 5,000 | 5,448 | 455 ms | 250 ms |
| 10,000 | 10,459 | 1,619 ms | 438 ms |
| 20,000 | 20,469 | 4,322 ms | 220 ms |
20,000 with asyncRead: false (control) |
438 | 348 ms | 351 ms |
An earlier run of the same program under full CPU load gave 2,104 ms at 10,000 and 6,127 ms at 20,000 pipes.
From the incident that led to #137501 (two tray apps had leaked about 74,000 dotnet_<guid> pipes), measured while Windows released the handles after the apps were closed:
| named pipes on the machine | first ConvertFrom-Json '{"a":1}' in a fresh Windows PowerShell |
|---|---|
| 74,553 | about 105 s (51 s of it CPU time) |
| 50,249 | 44 s |
| 34,401 | 25 s |
| 22,089 | 10 s |
| 19,654 | 6 s |
| 1,622 | 0.4 s |
A managed stack sampled at 8 s, 20 s and 35 s of such a slow start always showed System.Web.HttpRuntime.StaticInit → UnsafeNativeMethods.PerfCounterInitialize. ConvertFrom-Json '[1,2]' stayed fast (it does not reach that path); ConvertFrom-Json '{"a":1}' and ConvertTo-Json were slow.
Expected behavior
The start-up cost of System.Web.HttpRuntime in a non-ASP.NET process should not depend on how many named pipes other processes on the machine hold, or at least not this steeply. If it cannot be fixed in .NET Framework, a note in the .NET 11 documentation would help ("redirected process streams and async anonymous pipes are named pipes; a large number of them slows down System.Web initialization in .NET Framework processes, including the JSON cmdlets of Windows PowerShell 5.1").
Impact
- The symptom shows up far away from the cause: scripts, build steps, test runners and tool hooks that start
powershell.exeand parse JSON became 100 to 300 times slower, and nothing pointed at pipes or at the .NET 11 processes that held them. Test gates with a 100-second limit per PowerShell call failed for hours. - It needs no extreme number to become measurable: 10,000 pipes already cost about 1.5 to 2 seconds per PowerShell start.
Workarounds we use
- Find the process that holds the pipes:
[IO.Directory]::GetFiles('\\.\pipe\').Count, then the process with the highest handle count. - In Windows PowerShell scripts that start often, avoid
ConvertFrom-Json/ConvertTo-Json(and with themSystem.Web), or run them in PowerShell 7.
Activity
dotnet-policy-service commentedon Nov 13, 2026
Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.