[New-bugs-announce] [issue30394] smtplib leaves open sockets around if SMTPConnectError is raised in __init__

Joel Hillacre report at bugs.python.org
Wed May 17 19:25:01 EDT 2017


New submission from Joel Hillacre:

I am encountering a ResourceWarning about an unclosed socket when getting a non 220 response during connect() in __init__() of smtplib.SMTP. Attached are a client script causing this warning for me, a server script to cause the client to the warning and a patch that fixes the warning for me. My python version is Python 3.6.1 (default, Apr  7 2017, 09:32:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux. I had found previous related issue with similar symptom and remedy in issue #21641.


$ python3.6 test_server.py
connected by ('127.0.0.1', 53630)
$ 


$ python3.6 test_client.py
Traceback (most recent call last):
  File "test_client.py", line 2, in <module>
    smtp = smtplib.SMTP('127.0.0.1', 8025)
  File "/usr/lib64/python3.6/smtplib.py", line 253, in __init__
    raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (554, b'Nope.')
/usr/lib64/python3.6/socket.py:657: ResourceWarning: unclosed <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 53630), raddr=('127.0.0.1', 8025)>
$ 


RFC 2821 states that servers responding with non 220 greetings must not close the connection. It is this behaviour that test_server.py is using to trigger the warning in test_client.py.

RFC 2821 Section 3.1 Paragraph 3
https://tools.ietf.org/html/rfc2821#section-3.1
   '... a 554 response MAY be given in the initial connection opening message
   instead of the 220.  A server taking this approach MUST still wait
   for the client to send a QUIT (see section 4.1.1.10) before closing
   the connection and SHOULD respond to any intervening commands with
   "503 bad sequence of commands".'

The ResourceWarning is no longer caused for me after applying this change.

----------
components: Library (Lib)
files: smtplib.patch
keywords: patch
messages: 293905
nosy: jhillacre
priority: normal
severity: normal
status: open
title: smtplib leaves open sockets around if SMTPConnectError is raised in __init__
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file46873/smtplib.patch

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


More information about the New-bugs-announce mailing list