Bug 36414 - [Mono 4.2 Regression]: ThreadPool.SetMinThreads and ThreadPool.GetAvailableThreads don't works as expected
Summary: [Mono 4.2 Regression]: ThreadPool.SetMinThreads and ThreadPool.GetAvailableTh...
Status: VERIFIED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: (C6SR1)
Assignee: Ludovic Henry
URL:
Depends on:
Blocks:
 
Reported: 2015-12-01 12:15 UTC by Vlad
Modified: 2016-01-28 19:23 UTC (History)
6 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:
VERIFIED FIXED

Description Vlad 2015-12-01 12:15:25 UTC
Calling ThreadPool.SetMinThreads sets the maximum number of threads in the thread pool, same as SetMaxThreads.
Calling ThreadPool.GetAvailableThreads always returns the same values.

Here is the snippet of code I used to reproduce issue:
/************************************************************************/
class Program
{
	static void Main(string[] args)
	{
		int workerThreads, completionPortThreads;
		Console.WriteLine("SetMinThreads: {0}", ThreadPool.SetMinThreads(10, 10));
		ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
		Console.WriteLine("MinWorkerThreads = {0}, MinCompletionPortThreads = {1}", workerThreads, completionPortThreads);

		Console.WriteLine("SetMaxThreads: {0}", ThreadPool.SetMaxThreads(20, 20));
		ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
		Console.WriteLine("MaxWorkerThreads = {0}, MaxCompletionPortThreads = {1}", workerThreads, completionPortThreads);

		ThreadPool.QueueUserWorkItem(Callback, null);

		while (workerThreads > 0)
		{
			ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
			Console.WriteLine("workerThreads = {0}, completionPortThreads = {1}", workerThreads, completionPortThreads);
			Thread.Sleep(1000);
		}
	}

	public static void Callback(object nullState)
	{
		ThreadPool.QueueUserWorkItem(Callback, null);
		ThreadPool.QueueUserWorkItem(Callback, null);
		ThreadPool.QueueUserWorkItem(Callback, null);
		ThreadPool.QueueUserWorkItem(Callback, null);
		Thread.Sleep(Timeout.Infinite);
	}
}
/************************************************************************/

Mono 4.2.1 output:
----------
SetMinThreads: True
MinWorkerThreads = 1, MinCompletionPortThreads = 1
SetMaxThreads: True
MaxWorkerThreads = 20, MaxCompletionPortThreads = 20
workerThreads = 20, completionPortThreads = 20
workerThreads = 20, completionPortThreads = 20
workerThreads = 20, completionPortThreads = 20
workerThreads = 20, completionPortThreads = 20
workerThreads = 20, completionPortThreads = 20
....
----------


Mono 4.0.1 output (works as expected):
----------
SetMinThreads: True
MinWorkerThreads = 10, MinCompletionPortThreads = 10
SetMaxThreads: True
MaxWorkerThreads = 20, MaxCompletionPortThreads = 20
workerThreads = 19, completionPortThreads = 20
workerThreads = 10, completionPortThreads = 20
workerThreads = 8, completionPortThreads = 20
workerThreads = 6, completionPortThreads = 20
workerThreads = 4, completionPortThreads = 20
workerThreads = 2, completionPortThreads = 20
workerThreads = 0, completionPortThreads = 20
----------
Comment 1 Ludovic Henry 2015-12-10 11:26:41 UTC
Fixed on master with https://github.com/mono/mono/commit/780fbf4a57c1732e136d7ff80e61c6468e587df6, backporting it to 4.2
Comment 2 Arpit Jha 2015-12-15 05:49:32 UTC
I have checked this issue with latest mono master MonoFramework-MDK-4.3.0.2063.macos10.xamarin.universal_dcb167189d14e2b55ccd128bfc2b5e5483ea0808 and observed that workerThreads reaches to 0 in console as mentioned in bug description expected behaviour.

Screencast: http://www.screencast.com/t/T3mU3mj5XM

Environment Info: https://gist.github.com/Arpit360/d8cfc92ccb44ccea166b

Hence closing this issue.
Comment 3 Arpit Jha 2016-01-15 05:46:43 UTC
I have checked this issue with latest C6SR1 Mono 4.2.2.28 and workerThreads reaches to 0 in console, working as expected.

Screencast: http://www.screencast.com/t/ZfWkpqtdwzza
Environment Info: https://gist.github.com/Arpit360/2fd3b86736341972ac56