NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Running gateway with multiple channels exhausting thread pool

Open chrisbednarski opened this issue 14 years ago • 11 comments

Under high load, the HTTP gateway receiver (HttpListener) blocks for 60 seconds at a time and doesn't accept any more requests. After 60 seconds, number of distributed transactions time out and the process locks up again. Once in this state, no more messages get through the channel (ie complete the process of de-duplication/enqueuing messages). The issue was found on 3.2.8, but can also be replicated on 3.3.0

I've traced the blocking problem down to a call made by NServiceBus.Gateway.Channels.Http.HttpChannelReceiver

https://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway/Channels/Http/HttpChannelReceiver.cs#L26

It appears my process exhausts the thread pool workers while trying to service the HttpListener, commit distributed transactions (deduplication + enqueue MSMQ) and service my WCF requests at the same time.

Number of threads in the thread pool is a process wide setting. The OS (W7SP1) defaults for the number of thread pool workers is around 1000. Based on the above gateway config, HttpChannelReceiver limits it down to 10 workers. The gateway channel configuration treats it as if it was configurable per channel and affects performance of the entire process.

I'd be eager to remove the call to ThreadPool.SetMaxThreads altogether. Is this a bug? Why is the gateway receiver trying to limit the number of requests processed on a channel?

chrisbednarski avatar Oct 30 '12 01:10 chrisbednarski

@chrisbednarski very good question, the code should not be restricting a global setting!

I looked at the history and that seems to be there for at least 2years.

@andreasohlund any ideas why we would be doing this? Can we remove it?

johnsimons avatar Oct 30 '12 01:10 johnsimons

There is a very easy workaround - callThreadPool.SetMaxThreads()after the bus & gateway have started up

chrisbednarski avatar Oct 30 '12 02:10 chrisbednarski

@chrisbednarski yes that is a workaround. But we still need to fix this!

Feel like sending us a patch?

johnsimons avatar Oct 30 '12 02:10 johnsimons

Removing a single line of code is easy so I could send a patch, but I'd like to hear some reasoning behind it first.

chrisbednarski avatar Oct 30 '12 02:10 chrisbednarski

Well the reason is easy, it is because we using ThreadPool.QueueUserWorkItem in the code and we provide a way for the users to control the number of threads used by the channed, see https://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway.Tests/App.config#L19

So where the problem is, is that we should not be using the global ThreadPool for this and instead we need to create our own thread pool.

So to fix this we need to modify this code to not use the .net ThreadPool to manage threads.

With .Net 4 we can now use Tasks and our own TaskScheduler that limits the MaximumConcurrencyLevel, so it should be pretty easy to change this :smile:

johnsimons avatar Oct 30 '12 02:10 johnsimons

Agree with John, that is a misstake that we need to fix

On Tue, Oct 30, 2012 at 3:28 AM, John Simons [email protected]:

Well the reason is easy, it is because we using ThreadPool.QueueUserWorkItemhttps://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway/Channels/Http/HttpChannelReceiver.cs#L121in the code and we provide a way for the users to control the number of threads used by the channed, see https://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway.Tests/App.config#L19

So where the problem is, is that we should not be using the global ThreadPool for this and instead we need to create our own thread pool.

So to fix this we need to modify this code to not use the .net ThreadPool to manage threads.

With .Net 4 we can now use Tasks and our own TaskScheduler that limits the MaximumConcurrencyLevelhttp://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.maximumconcurrencylevel.aspx, so it should be pretty easy to change this [image: :smile:]

— Reply to this email directly or view it on GitHubhttps://github.com/NServiceBus/NServiceBus/issues/716#issuecomment-9893087.

http://andreasohlund.net http://twitter.com/andreasohlund

andreasohlund avatar Oct 30 '12 06:10 andreasohlund

Looking at http://support.microsoft.com/kb/821268, HttpWebRequest.GetResponse throws System.InvalidOperationException saying There were not enough free threads in the ThreadPool object to complete the operation. This would've been much easier to find if gateway didn't swallow the exception here https://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway/Channels/Http/HttpChannelReceiver.cs#L129

Could the exception be logged instead?

Edit: looks like I confused HttpWebRequest.GetResponse with HttpListener.GetContext. Call to GetContext() waits patiently forever and no exception is ever thrown unless the listener is disposed of.

@johnsimons: TaskScheduler is an abstract class. NSB would have to take dependency on ParallelExtensionsExtras (which appears to be Microsoft sample code) to use one of their schedulers (eg. http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=44488&pathId=1111204181) or write it's own TaskScheduler :( See http://blogs.msdn.com/b/pfxteam/archive/2010/04/09/9990424.aspx for more details.

chrisbednarski avatar Oct 30 '12 21:10 chrisbednarski

Definitely, good catch(by you, not the code :))

Sent from my iPhone

On 30 okt 2012, at 22:14, chrisbednarski [email protected] wrote:

Looking at http://support.microsoft.com/kb/821268, HttpWebRequest.GetResponse throws System.InvalidOperationException saying There were not enough free threads in the ThreadPool object to complete the operation. This would've been much easier to find if gateway didn't swallow the exception here https://github.com/NServiceBus/NServiceBus/blob/master/src/gateway/NServiceBus.Gateway/Channels/Http/HttpChannelReceiver.cs#L129

Could the exception be logged instead?

— Reply to this email directly or view it on GitHubhttps://github.com/NServiceBus/NServiceBus/issues/716#issuecomment-9923161.

andreasohlund avatar Oct 30 '12 21:10 andreasohlund

I'm getting a similar situation occurring while using NHibernate. A number of transactions are timing out in the DTC. When this happens I run out of connections in the SQL connection pool. I assume this is happening because the timed out transactions are holding onto the connection...

Could this be the same problem?

RossJayJones avatar Nov 12 '12 16:11 RossJayJones

Are you using the gateway? (with the nh persister)

Given that the DTC tx times out the connections should be returned to the pool??

Can it be this issue - https://nhibernate.jira.com/browse/NH-3023

On Mon, Nov 12, 2012 at 11:50 AM, RossJayJones [email protected]:

I'm getting a similar situation occurring while using NHibernate. A number of transactions are timing out in the DTC. When this happens I run out of connections in the SQL connection pool. I assume this is happening because the timed out transactions are holding onto the connection...

Could this be the same problem?

— Reply to this email directly or view it on GitHubhttps://github.com/NServiceBus/NServiceBus/issues/716#issuecomment-10294875.

http://andreasohlund.net http://twitter.com/andreasohlund

andreasohlund avatar Nov 12 '12 19:11 andreasohlund

That does seem to be closer to the problem as we are not currently using the Gateway. Thanks for the link!

RossJayJones avatar Nov 12 '12 19:11 RossJayJones