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)

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


Attachments

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 ***

Note You need to log in before you can comment on or make changes to this bug.