[ python-Bugs-808164 ] socket.close() doesn't play well with __del__

SourceForge.net noreply at sourceforge.net
Wed Feb 25 15:38:45 EST 2004


Bugs item #808164, was opened at 2003-09-17 16:16
Message generated for change (Comment added) made by jbrouwers
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=808164&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Perry Greenfield (perrygreenfield)
Assigned to: Nobody/Anonymous (nobody)
Summary: socket.close() doesn't play well with __del__

Initial Comment:
It appears that 2.3 changed how socket.close() works. 
We now find errors when terminating Python when we 
reference socket.close() in a __del__ method. We have 
worked around it by removing calls to socket.close() but 
note the following differences between the same section 
of code between 2.2 and 2.3 which suggest that the 2.3 
behavior is not ideal and should be fixed.

In Python 2.3:

    def close(self):
        self._sock = _closedsocket()
        self.send = self.recv = self.sendto = self.recvfrom                         = self._sock._dummy

In Python 2.2:

    def close(self):
        # Avoid referencing globals here
        self._sock = self.__class__._closedsocket()

Note the reference to avoiding globals which is 
presumably the source of the problem with 2.3. Perhaps 
I'm naive but I would guess that calling a close method 
should be considered a safe operation for use in a 
__del__ method.

----------------------------------------------------------------------

Comment By: Jean M. Brouwers (jbrouwers)
Date: 2004-02-25 14:38

Message:
Logged In: YES 
user_id=832557

Two other comments on socket.close() in Python 2.3.

In addition, the socket.close() method in the Lib/socket.py
class does not even call the _realsocket.close() method.

Before zapping the _sock attribute, shouldn't the latter
close() method be called first?  If not, what happens to
any data which may be buffered by the _realsocket?


Also, every time the socket.close() method is called
another, new instance of the _closedsocket class is
created.  That seems unnecessary waste.

Why not use a single instance created once at the
module level?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=808164&group_id=5470



More information about the Python-bugs-list mailing list