Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Does NOT Have Reliable Asynchronouos Socket Communication?

I once wrote a Crawler in .NET. In order to improve its scalability, I tried to take advantage of asynchronous API of .NET.

The System.Net.HttpWebRequest has asynchronous API BeginGetResponse/EndGetResponse. However, this pair of API is just to get a HTTP response headers and a Stream instance from which we can extract HTTP response content. So, my strategy is to use BeginGetResponse/EndGetResponse to asynchronously get the response Stream, then use BeginRead/EndRead to asynchronously get bytes from the response Stream instance.

Everything seems perfect until the Crawler goes to stress test. Under stress test, the Crawler suffers from high memory usage. I checked the memory with WinDbg+SoS and fount out that lots of byte arrays are pined by System.Threading.OverlappedData instances. After some searching in internet, I found this KB http://support.microsoft.com/kb/947862 from microsoft.

According to the KB, the number of asynchronous I/O should have a "upper bound", but it doesn't tell a "suggested" bound value. So, in my eye, this KB helps nothing. This is obviously a .NET bug. Finally, I have to drop the idea to do asynchronous extracting bytes from response Stream, and just do it in synchronous way.

The .NET library that allows Asynchronous IO with dot net sockets (Socket.BeginSend / Socket.BeginReceive / NetworkStream.BeginRead / NetworkStream.BeginWrite) must have an upper bound on the amount of buffers outstanding (either send or receive) with their asynchronous IO.

The network application should have an upper bound on the number of outstanding asynchronous IO that it posts.

Edit: Add some question marks.

Anybody has any experience to do asynchronous I/O on Socket & NetworkStream? Generally speaking, does crawler in production do I/O with internet with Synchronous or Asynchronosly?

like image 875
Morgan Cheng Avatar asked Oct 25 '08 09:10

Morgan Cheng


3 Answers

Hmya, this is not a .NET framework problem. The linked KB article could have been a bit more explicit: "you're using a loaded gun, this is what happens when you aim it at your foot". The bullets in that gun are .NET giving you the ability to start as many asynchronous I/O requests as you dare. It will do what you ask it to do, until you hit some kind of resource limit. In this case, probably, having too many pinned receive buffers in the generation 0 heap.

Resource management is still very much our job, not .NET's. It is no different from allocating memory without bound. Solving this particular problem requires you to put a limit on the number of uncompleted BeginGetResponse() requests. Having hundreds of them make little sense, every one of them has to squeeze through the Intertube one at a time. Adding another request will just cause it to take longer to complete. Or crash your program.

like image 93
Hans Passant Avatar answered Oct 25 '22 08:10

Hans Passant


You obviously want to limit the number of concurrent requests, no matter if your crawler is synch/asynch. That limit is not fixed, it depends on your hardware, network, ...

I'm not so sure what's your question here, as .NET implementation of HTTP/Sockets is "ok". There are some holes (See my post about controlling timeouts properly), but it gets the job done (we have a production crawler that fetches ~ hundreds of pages per second).

BTW, we use synchronous IO, just for convenience sake. Every task has a thread, and we limit the number of concurrent thread. For thread-management, we used Microsoft CCR.

like image 36
ripper234 Avatar answered Oct 25 '22 09:10

ripper234


This isn't limited to .Net.

It's a simple fact that each async request (file, network, etc) uses memory and (at some point, for networking requests at least) non paged pool (see here for details of the problems you can get in unmanaged code). The number of outstanding requests is therefore limited by the amount of memory. Pre-Vista there were some seriously low non paged pool limits that would cause you problems well before you ran out of memory, but in a post-vista environment things are much better for non paged pool usage (see here).

It's a little more complex in managed code as, in addition to the issues you get in the unmanaged world, you also have to deal with the fact that the memory buffers you use for async requests are pinned until those requests complete. Sounds like you're having these problems with reads, but it's just as bad, if not worse, for writes (as soon as TCP flow control kicks in on a connection those send completions are going to start taking longer to occur and so those buffers are pinned for longer and longer - see here and here).

The problem isn't that the .Net async stuff is broken, just that the abstraction is such that it makes it all look much easier than it really is. For example, to avoid the pinning issue, allocate all of your buffers in a single, large contiguous block at program start up rather than on demand...

Personally I'd write such a crawler in unmanaged code, but that's just me ;) You will still face many of the issues, but you have a bit more control over them.

like image 43
Len Holgate Avatar answered Oct 25 '22 10:10

Len Holgate