Bug 18686 - More threads are created than system can handle
Summary: More threads are created than system can handle
Status: RESOLVED DUPLICATE of bug 42945
Alias: None
Product: Runtime
Classification: Mono
Component: JIT (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-03-31 20:09 UTC by Marek Safar
Modified: 2017-10-14 00:35 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 42945

Description Marek Safar 2014-03-31 20:09:44 UTC
using System.Threading.Tasks;

class MainClass
{
	public static void Main (string[] args)
	{
		while (true) {
			Task.Run (() => {
				string s = "";
				for (int i = 0; i < 20000; ++i) {
					s += i.ToString ();
				}
			});
		}
	}
}

CreateThread: Error creating native thread handle Resource temporarily unavailable (35)

Unhandled Exception:
System.ExecutionEngineException: Couldn't create thread
[ERROR] FATAL UNHANDLED EXCEPTION: System.ExecutionEngineException: Couldn't create thread
Comment 1 Zoltan Varga 2014-04-02 02:11:51 UTC
This seems to be caused by a problem in the sgen heap expansion heuristics, if you run with
MONO_LOG_LEVEL=debug
The major heap/lost just seems to expand without limit.
Comment 2 Mark Probst 2014-04-02 18:27:34 UTC
The problem here is that tasks get allocated very quickly, but they complete incredibly slowly.  With the original test case, it seems that no task ever completes, in fact.  I have modified it a bit, to count started and completed tasks, and to do fewer iterations in the tasks:

using System;
using System.Threading.Tasks;

class MainClass
{
	static object completedLocker = new Object ();
	static long started = 0;
	static long completed = 0;

    public static void Main (string[] args)
    {
	    int j = 0;
	    while (true) {
		    Task.Run (() => {
				    string s = "";
				    for (int i = 0; i < 20000; ++i) {
					    s += i.ToString ();
				    }
				    lock (completedLocker)
				    {
					    ++completed;
				    }
			    });

		    ++started;

		    if ((++j % 100) == 0)
		    {
			    long c = 0;
			    lock (completedLocker)
			    {
				    c = completed;
			    }
			    Console.WriteLine ("started " + started + " completed " + c);
		    }
	    }
    }
}

After about 10 seconds, this program gets to

started 3649200 completed 86

With Boehm, it gets to

started 108300 completed 233

If we want this program to run, we will probably need to throttle threadpool enqueuing.
Comment 5 Marek Safar 2014-04-10 07:22:25 UTC
About 9 seconds to start 7 million tasks
Comment 6 Rodrigo Kumpera 2017-10-14 00:35:05 UTC
This is part of the theme of our TP not being competitive.

*** This bug has been marked as a duplicate of bug 42945 ***