0

I have a F# .NET process running an algorithm that performs many calculations. Due to combinatorics, the number of calculations can be several billions.

To reduce execution times, calculations are performed in parallel by using Async.Parallel(max parallelism = nbr of cores) |> Async.RunSynchronously

The process terminates correctly and the results are ok, however in the log I get multiple warnings like

 As of "07/26/2023 22:36:09 +00:00", the heartbeat has been running for "00:04:34.0765437" which is longer than "00:00:01". This could be caused by thread pool starvation. # {"EventId": {"Id": 22, "Name": "HeartbeatSlow"}, "SourceContext": "Microsoft.AspNetCore.Server.Kestrel"}

When this happens, the process hangs up for a variable time (from few seconds up to 10 minutes), and then it resumes and continunes.

Does anyone know the cause for this and how to fix it?

Franco Tiveron
  • 2,364
  • 18
  • 34

1 Answers1

1

Sounds like ThreadPool starvation, which "occurs when the pool has no available threads to process new work items and it often causes applications to respond slowly." See this article and this article and this SO question for details.

ThreadPool.SetMinThreads might be helpful as a workaround.

Brian Berns
  • 15,499
  • 2
  • 30
  • 40
  • I don't see how these apply. The process is not a web server, only a console app that runs in the background. No serving web requests. To avoid too many rquests, I am using the bounded version of Async.Parallel with max parallelism limited to 16. There is no thread locking calls (no Sleep, no database access, no IO of any kind, except serilog). Am I missing something? – Franco Tiveron Jul 27 '23 at 07:45
  • 1
    The log message you posted says you're using [Kestrel](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel), which is an ASP.NET Core web server. – Brian Berns Jul 27 '23 at 12:43
  • True, Kestrel is started but remains completely idle in this execution – Franco Tiveron Jul 27 '23 at 22:52
  • OK, it sounds like your console app is starving Kestrel by using up all available CPU time, which causes it to write that message to the log. Perhaps the console app appears to stop working during garbage collection? It's hard to know without more details. – Brian Berns Jul 28 '23 at 04:15