Author jhillacre
Recipients jhillacre
Date 2017-05-17.23:25:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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
connected by ('', 53630)

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

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

RFC 2821 Section 3.1 Paragraph 3
   '... 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 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.
Date User Action Args
2017-05-17 23:25:01jhillacresetrecipients: + jhillacre
2017-05-17 23:25:01jhillacresetmessageid: <>
2017-05-17 23:25:01jhillacrelinkissue30394 messages
2017-05-17 23:25:01jhillacrecreate