Issue with slow memory leak - Fixed - [STAThread]

Last week I noticed one of our RO services (which is a Windows service) leaked memory.

More specifically thread handles kept getting created (not threads, just the handles)

After a few months a million handles which then can deplete the entire memory and result in arbitrary applications crashing.

It took me a week to strip down the service and retest before I finally found the cause.

The cause is not (directly) remobjects related but since I lost a huge amount of time I am posting this case so it might help others.

The problem was hard to detect since it does not occur when running as an application (from debugger or normal) but only occurs wehn the application is run as a service.
Furthermore 1 of the 15 machines I tested on does not have the issue.

In the screenshot below you can see the handles (& large working set/private bytes) all related to thread handles.

After some investigation I believe that it must be some finalizer which is blocked because when calling GC.WaitForPendingFinalizers(); the function hangs

By removing code and iteratively deploying the stripped down versions I could see the problem also occurs for services that do virtually nothing (no RO calls are made).
The problem remained until the version where I removed the [STAThread] attribute form my Main function. This attribute was there for historic reasons but has never been an issue (that we noticed) since the application was created in 2019.

So in short: It appears to be important that the main thread uses MTA (multi threaded apartment COM model) instead of STA when running an application as a service.

For the interested people I’ll attach the simplified code which still suffers from the issue

FYI: Code that suffers from the issue (on most machines)
Note: The NLog log line is necessary but replacing that by some other code also triggers the issue.

OffNetServerTest.zip (6.6 MB)

Very curious! good catch. We should probably work the templates to account for that. Which version/type of .NET is this happening on, classic, .NET Core, or both?

thanx,
marc

This is .NET FW4.7.2 for us

The issue occurs on W7,W8, W10 and W11 machines.
It will not be related to a windows update or KB

The leak is very slow so the issue has probably been present dormantly for a while.
[I am currently checking if a build from 03/2022 already had the issue]