IOCP, AcceptEx, overlapped and WSAEINVAL

467 views Asked by At

I have a server that uses IOCPs, sockets and overlapped. Initially everything is just wonderful. The listening socket hands off to a newly created socket using AcceptEx on an IOCP. I can handle thousands of connections just fine.

When the server process falls behind in processing, it will close and disconnect the listening port. When it catches back up, it will reestablish the listening port with a new IOCP.

The issue I have run into is that on after reestablishing the listening port, and a new connection arrives, I attempt to accept using the exact same code path as above. The AcceptEx fails with WSAEINVAL.

I know I have left out some details (and the devil is always in the details, no?) -- but would appreciate assistance on where I should be looking.

If a curious soul would like more information, I'd be happy to supply.

2

There are 2 answers

2
Len Holgate On

It's hard to guess at what your problem might be given you don't show any source code, but...

  1. There's no need to close the listening socket, simply stop posting new AcceptEx() calls and the server will not be able to accept any new connections.

  2. if you really want to close the listening socket as well then do not close the IOCP and make sure you use the same IOCP when you recreate the listening socket.

0
user2097370 On

I will answer my own question, because I have figured out what the underlying issue was. One thing that was critical to the issue, but was not stated in the problem statement was that the server had sub-processes.

It turns out that while the default behavior in windows is to not have handles inherited by sub-processes, the behavior of winsock is the opposite: handles are inherited by sub-processes unless explicitly set to no-inherit on creation.

Creating sockets with non-inheritable handles solves this problem. I hope that this helps someone out there that runs into this issue.