Socket being garbage collected too early

Steve Holden steve at holdenweb.com
Thu Dec 16 19:07:41 EST 2004


Scott Robinson wrote:

> I have been having trouble with the garbage collector and sockets.
> Unfortunately, google keeps telling me that the problem is the garbage
> collector ignoring dead (closed?) sockets instead of removing live
> ones.  My problem is
> 
> 
> 	x.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> 	do_stuff(x.sock)
> 
> 
> def do_stuff(sock):
> 	sock_list.append(sock)
> 
> 
> once do_stuff finishes, x.sock disappears, and I can only believe it
> is being garbage collected.  I'd like to hear the standard means for
> avoiding this issue (gc appears to have only the interface to declare
> something garbage, not to declare something not garbage).
> 
> Scott Robinson
> 
Unfortunately, assuming it's being garbage collected might turn out to 
be incorrect. What evidence do you have that the socket "disappears"? Do 
you get a segmentation ault, or what?

If the socket simply fails to work that would be a different case 
altogether, but it seems to me that we need a bit more evodence that the 
anecdotal stuff you've provided so far.

Quite apart from anything else, by the way, the code you posted appears 
to use a global sock_list. A reference b y that list would in any case 
stop the socket from being garbage collected (quite apart from the fact 
that the socket module itself will do so as long as the socket is open).

So, could we see an error message, or some other evidence of what is 
going on. For example, after the call to do_stuff(), what do you see if you

     print sock_list

for example. I think your initial hypothesis is insufficient.

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list