Do Completion Ports Leak Memory? 
Author Message
 Do Completion Ports Leak Memory?

This question is primarily for Andy, though if anyone else has played with
this stuff, feel free to chime in!

I've been working on a set of classes to handle completion ports with
Winsock.  So far, so good.  I've managed to plug up pretty much all of the
memory leaks, but it looks like something is still not being deallocated
properly.  It's a very small leak (less than half a meg after 82,000
connections and several gigabytes of data transfer) but it's still gotta go.
In addition, after around 82,000 connections, the server stops responding.
The only light that's going on in my head at this point is that the WinAPI
doesn't provide a way to remove a socket from a completion port - at least
according to the documentation I have.  Would this result in the type of
leak I am describing?  Could a completion port ever get full of old,
disconnected sockets and stop functioning?  Adding a socket to a completion
port has to result in data being stored somewhere in memory, doesn't it?  If
it does, is there any way to clean this up after a client disconnects?
Thanks!

Dan Gorrell



Wed, 14 Sep 2005 07:46:55 GMT  
 Do Completion Ports Leak Memory?
Hi Dan,

Quote:
> This question is primarily for Andy, though if anyone else has played with
> this stuff, feel free to chime in!

:)

Quote:
> I've been working on a set of classes to handle completion ports with
> Winsock.  So far, so good.  I've managed to plug up pretty much all of the
> memory leaks, but it looks like something is still not being deallocated
> properly.  It's a very small leak (less than half a meg after 82,000
> connections and several gigabytes of data transfer) but it's still gotta
go.
> In addition, after around 82,000 connections, the server stops responding.
> The only light that's going on in my head at this point is that the WinAPI
> doesn't provide a way to remove a socket from a completion port - at least
> according to the documentation I have.  Would this result in the type of
> leak I am describing?  Could a completion port ever get full of old,
> disconnected sockets and stop functioning?  Adding a socket to a
completion
> port has to result in data being stored somewhere in memory, doesn't it?
If
> it does, is there any way to clean this up after a client disconnects?

Associating a socket with a completion port shouldn't be the issue although
MS would be the best people to ask. However any handles associated with an
IOCP port should be deassociated when the actual handle is freed that was
associated. All the IOCP does is check async request associated handles
against anything that is associated and pass it through to the
GetQueuedCompletionStatus call if it was an associated handle at the kernel
level. Presumably as there is no explicit way to deassociate, on freeing of
IOCP compatible handles, any association should be broken.

My advice would be to associate something like 1000 handles with an IOCP, do
no processing (remove that from the equation) and then free the handles and
see if you have a leak still. If you don't, the leak is in your processing
code. I would not have thought there would be a leak in IOCP given it's
wholesale use in the OS itself but you never know. My guess is that the
reason certain types of handles are compatible with IOCP is because they
have a duty to dissassociate themselves on being freed and hence any such
code is under the control of the Kernel. My guess is that the IOCP and the
handle both have a property identifying each other, in the case of the IOCP,
some kind of lookup table. Freeing the handle should remove the association.
However if you failed to free the socket handles you would have the leak as
you describe. I have not seen any leaks in IOCP and we put 000,000's of
requests through our web server.

HTH. By all means email directly if you need quicker responses than on the
NG's. I am coding an IOCP server at the moment for a data driven workflow
based system (although in C++) so I can certainly check this out. My guess
is that the leak is in your processing code though. If you have PlugIT, and
you're coding this in clarion, I could add leak checking support specific to
the handles and memory you're allocating which would tell you where the
problem is in your code.

Best Regards

Andy
Plugware Solutions.com Ltd,  taking the puzzle out of I.T
Tel : +44 1249 813335
Fax : +44 1249 813462



Thu, 15 Sep 2005 01:07:46 GMT  
 Do Completion Ports Leak Memory?
Thanks for the pointers Andy!  Your input has been very helpful!

It looks like what's happening is that some of my sockets aren't actually
disconnecting...  One thing I'm testing this against is I.E. (this
implementation speaks HTTP).  Every 3-5000 requests or so, I.E. crashes.
(Great bit of programming M.S.)  When it does, the sockets that were
servicing that connection don't disconnect and the associated Client object
doesn't get disposed.  It looks like now is the time to beef up the error
management part of my Winsock implementation.  :)

Another option would be for the server to periodically check for "stale"
sockets and disconnect them.  I'm still thinking through this approach,
since I don't want to disconnect clients who should still have a connection,
even if they haven't transmitted data for a while.  In HTTP, it wouldn't be
a huge problem, but I want the classes themselves to be useable for any
protocol.  One of my next steps will be to implement Keep-Alive over HTTP,
so I guess now is a good time to figure this out anyway. :)

Dan Gorrell



Sat, 17 Sep 2005 01:02:45 GMT  
 Do Completion Ports Leak Memory?
Hi Dan,

Quote:
> Thanks for the pointers Andy!  Your input has been very helpful!

You're welcome.

Quote:
> It looks like what's happening is that some of my sockets aren't actually
> disconnecting...  One thing I'm testing this against is I.E. (this
> implementation speaks HTTP).  Every 3-5000 requests or so, I.E. crashes.
> (Great bit of programming M.S.)  When it does, the sockets that were
> servicing that connection don't disconnect and the associated Client
object
> doesn't get disposed.  It looks like now is the time to beef up the error
> management part of my Winsock implementation.  :)

I thought it would be the sockets. Yeah, you should be able to check for the
disconnect / inactivity in a number of ways. Some software uses pings with a
timeout which not only keeps the connection up but also allows you to easily
determine whether the client is still there however things like this are not
always practical unles the software is custom on both ends. So in the case
of IE this is no good. Security and firewalls are something else that need
to be considered. Timeouts by themselves are generally the order of
business. i.e after 15 mins of inactivity, disconnect the socket. then users
away from thier machines are essentially leaving them secure'ish knowing
that the connection will be severed. where this is not appropriate is
systems where users will have long periods of inactivity. It's a tough
choice. Keep alive should really help here.

Quote:
> Another option would be for the server to periodically check for "stale"
> sockets and disconnect them.  I'm still thinking through this approach,
> since I don't want to disconnect clients who should still have a
connection,
> even if they haven't transmitted data for a while.  In HTTP, it wouldn't
be
> a huge problem, but I want the classes themselves to be useable for any
> protocol.  One of my next steps will be to implement Keep-Alive over HTTP,
> so I guess now is a good time to figure this out anyway. :)

Keep alive is good in an application type of scenario where you have a
limited number of users hitting each server in a farm of machines but can
get resource hungry if not controlled.

Regards

Andy
Plugware Solutions.com Ltd,  taking the puzzle out of I.T
Tel : +44 1249 813335
Fax : +44 1249 813462



Sat, 17 Sep 2005 03:14:50 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. (Port-induced?) memory leak in 2.0

2. com port problems and memory leak?

3. memory leak and leak-fixing 'patterns'

4. python startup memory size and memory leak

5. Uninitialized memory errors and memory leaks in Tk

6. memory usage (how to debug a memory leak?)

7. Possible Dolphi R4 memory leak using ODBC

8. GETDSAB and memory leaks

9. How to pinpoint memory leaks

10. TopLink errors and memory leaks

11. memory leak in gawk 3.1.0 ?

12. ENFIN Memory Leak

 

 
Powered by phpBB® Forum Software