1

Are pending asynchronous operations are precious resource that should be used sparingly?

I have a slew of connected TcpClients that I'm not expecting input from, but just in case one is misbehaving, I need to keep the input buffers empty. If I have a BeginRead() for each connected client that just stays open indefinitely, is there any harm? The callback will be called when the client is eventually closed and I can just return out of it if !asyncResult.IsCompleted.

I wouldn't know how many bytes to begin reading, though I believe I could share one buffer. Input up to that buffer limit could just sit in the stream, unread. Is there a better way to efficiently discard, disallow, or WAIT on the availability of data in a stream?

Otherwise I need to poll for data available.

Thanks!

2 Answers 2

3

It depends on the operating system that you're running on...

A pending recv will use some non-paged pool and it will cause some pages of memory to be locked for I/O.

Prior to windows Vista, non-paged pool was pretty scare, and it's a machine wide commodity which can cause poorly designed drivers to crash if it runs out... So on pre-Vista OS's you may get 'ENOBUFS' errors from the underlying overlapped I/O system due to lack of non-paged pool. See here: http://www.lenholgate.com/blog/2009/03/excellent-article-on-non-paged-pool.html for some more details on the non-paged pool limits.

The other resource that is used by a pending recv is one or more pages of memory are locked for i/o whilst the operation is pending. There's a finite limit on the number of pages that CAN be locked and so, once again, you might get an 'ENOBUFS' from the underlying I/O system in certain circumstances.

Of course it depends on how many connections you have as to whether these limits affect you or not.

The 'standard' way of dealing with the locked I/O pages limit in unmanaged code is to post zero byte reads, that is reads which have buffers which are actually 0 bytes long. This means that no buffer space is locked whilst the read is pending and when it completes you simply post a normal read, or read synchronously.

2
  • 1
    Reflector shows that the 0-byte read trick should work in managed code on both Socket and NetworkStream in synchronous and asynchronous mode. However you still need to pass a buffer and it will be pinned by .NET. Whether it will be locked by OS is a different matter. As for non-paged pool consumption, doesn't a synchronous recv use (almost) the same amount of non-paged pool? After all it's only the user code that sees it as 'synchronous' and the IRP processing is the same in both cases. Commented Feb 28, 2011 at 18:00
  • 1
    You use a sync read after a zero byte read returns. You know there's data so there's no need to go async. It uses the same non paged pool as far as I know Commented Feb 28, 2011 at 18:37
1

No, they are not. Besides the socket which you end up using anyway, an asynchronous operation uses an event — the most light-weight kernel object — and an OVERLAPPED structure, i.e. a few words of memory. I expect you will run out of sockets long before you run out of events or memory.

There appears to be no mechanism to wait for data without reading it in .NET (the underlying Win32 API allows one to do this, see WSAEventSelect), so you will have to read 1 byte. Sharing a single large buffer between multiple sockets, reading to different offsets, may be advisable because any buffer used for asynchronous i/o must stay pinned until i/o completes.

1
  • Thanks! I made an edit while you were commenting. I don't know how many bytes to asynchronously read. I could start with one and THEN start asking about how many are available, and if none, go back to 1. Is that advisable? Commented Feb 28, 2011 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.