This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author socketpair
Recipients socketpair
Date 2011-02-20.15:44:05
SpamBayes Score 1.4308347e-07
Marked as misclassified No
Message-id <1298216646.94.0.460064768402.issue11257@psf.upfronthosting.co.za>
In-reply-to
Content
asyncore.py:
-----------
    def add_channel(self, map=None):
        #self.log_info('adding channel %s' % self)
        if map is None:
            map = self._map
        map[self._fileno] = self
---------

As we see, it add itself to a map, creating unnecessary refences to 'self'.

Such code should be rewritten via weakref. Now it's unknown when object will garbage collected. For example, if someone forget to call close(), object will stuck, eating file descriptor and memory... 

When using weakref, we may guarantee (via callback fcuntion), that we call close() when last reference to object has been lost. Also, such behaviour guarantee, that object will be garbage collected when last user's reference has gone.

To approve my thoughts, see code:
------------------------------
class MyServer(asyncore.dispatcher):
    def __init__(self, listenaddr):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(listenaddr)
        self.listen(5)
    def handle_accept(self):
        while 1:
            r=self.accept()
            if r is None:
                break
            my_conn_handler(r[0])
------------
As we see, we create a new instance via my_conn_handler(), and we do not store reference to it, but object will not be garbage collected. It's unexpected behaviour.
History
Date User Action Args
2011-02-20 15:44:07socketpairsetrecipients: + socketpair
2011-02-20 15:44:06socketpairsetmessageid: <1298216646.94.0.460064768402.issue11257@psf.upfronthosting.co.za>
2011-02-20 15:44:05socketpairlinkissue11257 messages
2011-02-20 15:44:05socketpaircreate