[issue12378] smtplib.SMTP_SSL leaks socket connections on SSL error

Joe Shaw report at bugs.python.org
Mon Jun 20 21:44:24 CEST 2011


New submission from Joe Shaw <jshaw at itasoftware.com>:

Start a non-SSL server on port 2525:

$ python -m smtpd -n -c DebuggingServer localhost:2525

In another terminal, fire up a python interpreter and run the following code:

>>> import smtplib
>>> s = smtplib.SMTP_SSL("localhost", 2525)
[...]
ssl.SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

The underlying socket connection is still open, but you can't access it or close it:

$ lsof -P -p 76318 | grep 2525
Python  76318 joeshaw    3u  IPv4 0x09a9fb18       0t0      TCP localhost:64328->localhost:2525 (ESTABLISHED)

This wreaks havoc if you're trying to write a unit test using the smtpd module and asyncore in a thread and try to clean up after yourself.

The code inside SMTP_SSL looks something like this (on 2.6.5 anyway):

        def _get_socket(self, host, port, timeout):
            if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
            new_socket = socket.create_connection((host, port), timeout)
            new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
            self.file = SSLFakeFile(new_socket)
            return new_socket

Something like:

            new_socket = socket.create_connection((host, port), timeout)
            try:
                new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
            except:
                new_socket.close()
                raise
            self.file = SSLFakeFile(new_socket)
            return new_socket

I think will do the trick.

----------
components: Library (Lib)
messages: 138753
nosy: joeshaw
priority: normal
severity: normal
status: open
title: smtplib.SMTP_SSL leaks socket connections on SSL error
type: resource usage
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12378>
_______________________________________


More information about the Python-bugs-list mailing list