Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aspnet core 3.1 linux ThreadPool.SetMinThreads is not work #1872

Closed
gaojiandonggg opened this issue Jan 17, 2020 · 10 comments · Fixed by #57699
Closed

aspnet core 3.1 linux ThreadPool.SetMinThreads is not work #1872

gaojiandonggg opened this issue Jan 17, 2020 · 10 comments · Fixed by #57699
Labels
area-System.Threading question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@gaojiandonggg
Copy link

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

hello
aspnet core 3.1
int processorCount = Environment.ProcessorCount;
ThreadPool.SetMinThreads(processorCount * 5, processorCount * 100);
run in docker is not working by linux ,
but can working in windows ,why?
who can help me, thinks

@analogrelay
Copy link
Contributor

I don't think this is specific to ASP.NET Core. I'm going to transfer this to dotnet/runtime and see if someone there can help.

@analogrelay analogrelay transferred this issue from dotnet/aspnetcore Jan 17, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 17, 2020
@stephentoub
Copy link
Member

is not working by linux

How does that manifest?

What is the value of processorCount?

What is the return value of SetMinThreads?

@gaojiandonggg
Copy link
Author

For example, if the system has 4 cores, then processorCount should be 4. In Windows and Linux systems, I can normally get the value from a container of Linux docker run, but if I start the instance with k8s, it will display as follows:

ThreadPool.ThreadCount: 16, Minimum work threads: 16, Minimum completion port threads: 16)

It might have something to do with the k8s Settings

@stephentoub
Copy link
Member

Thanks, but I'm trying to understand what you mean by "is not working". What is the symptom that you're reporting? Is SetMinThreads returning false? Are you querying GetMinThreads after you call SetMinThreads and it's not reporting the values you passed in?

@jeffschwMSFT jeffschwMSFT added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Jan 22, 2020
@jeffschwMSFT jeffschwMSFT added this to the Future milestone Jan 22, 2020
@jeffschwMSFT
Copy link
Member

cc @kouvel

@dktue
Copy link

dktue commented Feb 5, 2021

We're having the same issue: It shows exactly like you told, @stephentoub .

@maxtyutmanov
Copy link

Same trouble on .net core 2.2.8 in docker under linux, using microsoft/dotnet:2.2-aspnetcore-runtime as base image.
The code snippet that we use for thread pool configuration:

private static void ConfigureThreadPool(IConfiguration configuration)
{
	ThreadPool.GetMinThreads(out var currentMinWorkerThreads, out var currentMinIoThreads);

	System.Console.WriteLine(
		"Current thread pool config: min worker threads - {0}, min IO threads - {1}", 
		currentMinWorkerThreads, currentMinIoThreads);

	var newMinWorkerThreads = configuration.GetValue<int?>("MinThreadPoolWorkerThreads") ?? currentMinWorkerThreads;

	System.Console.WriteLine("Reconfiguring thread pool: min worker threads - {0}", newMinWorkerThreads);

	var success = ThreadPool.SetMinThreads(newMinWorkerThreads, currentMinIoThreads);
	if (success)
	{
		ThreadPool.GetMinThreads(out currentMinWorkerThreads, out currentMinIoThreads);
		System.Console.WriteLine(
			"Successfully reconfigured thread pool: min worker threads - {0}, min IO threads - {1}",
			currentMinWorkerThreads, currentMinIoThreads);
	}
	else
	{
		System.Console.WriteLine("Could not reconfigure thread pool");
	}
}

and this is the output I am getting:

Current thread pool config: min worker threads - 80, min IO threads - 2
Reconfiguring thread pool: min worker threads - 128
Successfully reconfigured thread pool: min worker threads - 80, min IO threads - 2

The same code works fine when run locally on my Windows 10 machine.
I also tried setting System.Threading.ThreadPool.MinThreads in runtimeconfig.template.json, no success.
Any idea what could cause such behavior?

@kouvel
Copy link
Member

kouvel commented May 27, 2021

It sounds like it might already be configured somehow. Could be through System.Threading.ThreadPool.MinThreads, can you check in <app>.runtimeconfig.json in the build output folder? It may also be overridden using an environment variable COMPlus_ThreadPool_ForceMinWorkerThreads=0x50 using a hex value. Those settings would override the value set by ThreadPool.SetMinThreads despite it returning true.

@maxtyutmanov
Copy link

@kouvel COMPlus_ThreadPool_ForceMinWorkerThreads=0x50 was exactly the reason, thank you!
The behavior of SetMinThreads looks non-intuitive to me though: it reported success, but did not change the settings.

@kouvel
Copy link
Member

kouvel commented May 27, 2021

Yea looks like historical behavior, perhaps it was intended that returning true means that the values provided are valid rather than that the values were changed. Probably could fix.

kouvel added a commit to kouvel/runtime that referenced this issue Aug 18, 2021
…en they don't override the preconfigured value

Fixes dotnet#1872
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 18, 2021
kouvel added a commit that referenced this issue Aug 30, 2021
…en they don't override the preconfigured value (#57699)

* Fix `ThreadPool.SetMinThreads` and `SetMaxThreads` to return false when they don't override the preconfigured value

Fixes #1872
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 30, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Threading question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
9 participants