classification
Title: error responses from server are masked in smtplib when server closes connection
Type: behavior Stage: resolved
Components: Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: kushal.das, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2013-03-20 20:48 by r.david.murray, last changed 2014-04-14 22:24 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
issue17498.patch kushal.das, 2013-03-21 02:07 review
issue17498_v2.patch kushal.das, 2014-04-14 15:19 New version of the patch which can be successfully applied to tip. review
Messages (6)
msg184793 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-20 20:48
If an SMTP server returns an error code and then closes the connection, smtplib will attempt to send an RSET command, which will then raise a socket error, which smtplib turns into a ServerDisconnectedError, thereby loosing the information about what error code the server actually returned.  While (except for code 421, as noted in issue 5713) a server doing this is technically out of spec with the RFC, it is a reality of the internet that it happens, and smtplib should handle it correctly.
  
Since the RSET is just to get the protocol back to a known starting state after an error, I believe the fix is to have an internal _rset method that wraps the RSET call in a try/except and just ignores any ServerDiscoonected error.  The library would then call this internally instead of the public API method.  If the library client continues on after the error code it gets back from calling an smtplib method that results in the server closing the connection, it will get the ServerDisconnected error the next time it tries to send a command to the server.  Thus the behavior change introduced here is just to allow commands that end with server disconnects to return an error code instead of an (otherwise mysterious and quite possibly currently-mishandled) ServerDisconnected error.  Since any existing code will already need to handle errors from commands sent to servers that *don't* disconnect, I believe that this is a safe fix from a backward compatibility standpoint.
msg184810 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2013-03-20 22:51
Working on a patch for this.
msg184829 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2013-03-21 02:07
The following patch adds a _rset method which catches any SMTPServerDisconnected exception, also includes the corresponding test and test_421_from_mail_cmd assert statement typo fix.
msg216089 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2014-04-14 15:19
New version of the patch which can be successfully applied to tip.
msg216235 - (view) Author: Roundup Robot (python-dev) Date: 2014-04-14 22:22
New changeset 3c441e9ccf87 by R David Murray in branch '3.4':
#17498: Defer SMTPServerDisconnected errors until the next command.
http://hg.python.org/cpython/rev/3c441e9ccf87

New changeset 842014ab1c06 by R David Murray in branch 'default':
Merge #17498: Defer SMTPServerDisconnected errors until the next command.
http://hg.python.org/cpython/rev/842014ab1c06
msg216237 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-04-14 22:24
Thanks, Kushal.  Sorry it took so long to get this committed :)
History
Date User Action Args
2014-07-26 21:35:42r.david.murraylinkissue16005 superseder
2014-04-14 22:24:41r.david.murraysetstatus: open -> closed
versions: + Python 3.5, - Python 2.7, Python 3.2, Python 3.3
type: behavior
messages: + msg216237

resolution: fixed
stage: needs patch -> resolved
2014-04-14 22:22:52python-devsetnosy: + python-dev
messages: + msg216235
2014-04-14 15:19:54kushal.dassetfiles: + issue17498_v2.patch

messages: + msg216089
2013-03-21 02:07:22kushal.dassetfiles: + issue17498.patch
keywords: + patch
messages: + msg184829
2013-03-20 22:51:15kushal.dassetmessages: + msg184810
2013-03-20 20:49:00r.david.murraysetnosy: + kushal.das
2013-03-20 20:48:30r.david.murraycreate