Repository navigation
Description
Description
On Linux x64, .NET 10.0.12 leaks native memory each time that a cached TypeInitializationException is rethrown from native runtime code (InitClassSlow) and caught in managed code.
The leaked blocks are 3400 B posix_memalign allocations. They contain a CONTEXT and an EXCEPTION_RECORD (AllocateExceptionRecords, pal/src/exception/seh-unwind.cpp).
In our Kubernetes production deployment, the trigger is Npgsql (GSS encryption mode Prefer, SSL on) in a container without libgssapi_krb5.so.2.
Each new physical connection calls NegotiateAuthentication.GetOutgoingBlob. This rethrows the TypeInitializationException of Interop+NetSecurityNative. Npgsql catches it.
A service grew about 28 MiB/day. A heap dump showed about 44,600 leaked 3400 B blocks after 5 days. The managed heap was stable at about 55 MB.
Analysis
- The rethrow starts in
InitClassSlow, which throws the cachedTypeInitializationExceptionfrom native code. - In the dump, the leaked CONTEXT records were captured in
DispatchManagedException(PAL_SEHException& ex, bool isHardwareException)(vm/exceptionhandling.cpp). This function callsRtlCaptureContext(ex.GetContextRecord()). UNINSTALL_MANAGED_EXCEPTION_DISPATCHER_EX(vm/exceptmacros.h) moves thePAL_SEHExceptioninto the localexCopyand callsDispatchManagedException(exCopy, false). This call does not return. Managed EH resumes in the managed catch, so~PAL_SEHException()(FreeRecords) does not run.- As far as I can see, the
ExInfothat is created inDispatchManagedException(OBJECTREF, CONTEXT*, EXCEPTION_RECORD*)does not take ownership of these records on this path. Nothing frees them.
Related issues
- Revisit EH at P/Invoke boundary for QCalls #123482: possibly related.
- Exception storm due to GSS encryption in version 10 npgsql/npgsql#6416: users report GSS exception storms and memory growth with Npgsql 10 in containers.
GssEncryptionMode=Disablefixes it. - Npgsql 10.0.2 catches the
TypeInitializationExceptionand continues without GSS. But the exception still occurs for each new physical connection (see the Npgsql 10.0.3 results above). - Do not attempt kerberos if it's not installed npgsql/npgsql#6593 (open, milestone 10.0.4): Npgsql stops GSS attempts after the first failure. This removes the Npgsql trigger. The runtime leak stays for other code that rethrows a cached
TypeInitializationException.
Reproduction Steps
Use mcr.microsoft.com/dotnet/runtime:10.0-noble-chiseled (it has no libgssapi):
using System.Net.Security;
static long Rss() => long.Parse(File.ReadAllLines("/proc/self/status").First(l => l.StartsWith("VmRSS")).Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]);
void Once()
{
try { new NegotiateAuthentication(new NegotiateAuthenticationClientOptions { TargetName = "postgres/db" }).GetOutgoingBlob(ReadOnlySpan<byte>.Empty, out _); }
catch (TypeInitializationException) { }
}
for (int i = 0; i < 2000; i++) Once();
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
long r0 = Rss();
for (int i = 0; i < 40000; i++) Once();
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
Console.WriteLine($"RSS delta {Rss() - r0} kB");Results (.NET 10.0.12, linux-x64)
| Image | Calls | RSS delta | Per call |
|---|---|---|---|
| runtime:10.0-noble-chiseled | 20,000 | 82 MB | ~4.2 KB |
| runtime:10.0-noble-chiseled | 40,000 | 152 MB | ~3.9 KB |
Same loop with a managed throw/catch |
40,000 | 15 MB (not linear) | - |
End-to-end with Npgsql 10.0.3 (Pooling=false, SslMode=Require, PostgreSQL 17 with SSL, same image):
GssEncryptionMode |
Connections | RSS delta | Per connection | First-chance TIE |
|---|---|---|---|---|
| Prefer | 10,000 | 37 MB | ~3.8 KB | 10,000 |
| Prefer | 30,000 | 103 MB | ~3.5 KB | 30,000 |
| Disable | 30,000 | 5 MB | - | 0 |
The runs used amd64 emulation (Podman) on an arm64 host. The production service runs natively on linux-x64 and shows the same leak.
Expected behavior
Memory RSS stays at a predictable level for long-running dotnet containers, where the runtime throughs TypeInitializationException.
Actual behavior
A steady, uncontrollable growth of native memory (not managed heap), which eventually causes applications deployed in containerized environments to reach environment limits and become unstable or get killed.
Regression?
Yes
Known Workarounds
In our case: Set GssEncryptionMode=Disable in the Npgsql connection string.
Configuration
No response
Other information
Note: Claude Opus 5.5 (GitHub Copilot CLI) assisted in this analysis. A human examined and verified the results and the repro.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 6, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
Fix software EH memory leak on Unix
sventropy commentedon Oct 7, 2026
Addition for any future readers: We verified that just adding the native library
libgssapi_krb5.so.2via the Dockerfile is also a valid workaround to stop the exception from happening and - in our case - to stop RSS from steadily growing.This, of course, only applies to anyone facing the exact same driver for this root cause, which is
Ngsqlopening connections. So this is only to some extent related to this issue.Fix software EH memory leak on Unix (#135290)
Brimerland commentedon Oct 9, 2026
I was affected today by the same issue:
A test suite on Linux (see below) retrying a failed System.Drawing initializer 1.45M times grew past 5 GB and crashed.
Given that 9.0 and 10.0 are affected, could #135290 be considered for servicing in 10.0 (and 9.0), in addition to 11.0?
(Maybe even 8.0 ... LTS ends 10. Nov. 2026)
Independent repro of the same leak, without Npgsql/GSS:
A type whose static constructor throws, accessed repeatedly in try/catch. Minimal console project (no dependencies):
TypeInitLeakRepro.zip
Bytes per access (working set after forced GCs, 200,000 iterations):
GLIBC_TUNABLES=glibc.malloc.arena_max=1:glibc.malloc.tcache_count=0 and
DOTNET_GCHeapHardLimit leave the number unchanged (3,464-3,465).
[release/10.0] Fix software EH memory leak on Unix (#135546)
[release/11.0] Fix software EH memory leak on Unix (#135541)