classification
Title: smtplib leaves open sockets around if SMTPResponseException is raised in __init__
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: Claudiu.Popa, orsenthil, python-dev
Priority: normal Keywords: patch

Created on 2014-06-02 14:55 by Claudiu.Popa, last changed 2014-06-03 14:26 by orsenthil. This issue is now closed.

Files
File name Uploaded Description Edit
smtplib_resource_warning.patch Claudiu.Popa, 2014-06-02 14:55 review
issue21641.patch Claudiu.Popa, 2014-06-03 08:23 review
Messages (5)
msg219592 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-02 14:55
Hello!

I noticed that test_smtplib raises a ResourceWarning, tracking this to this piece of code:

   self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
                     HOST, self.port, 'localhost', 3)

What happens is that `SMTP.getreply` is called in `SMTP.__init__` after the socket was opened, but if getreply raises SMTPResponseException, the socket remains opened. The attached patch fixes this.
msg219593 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-02 14:57
An alternative to this approach would be to catch the error in __init__ and close the socket there.
msg219669 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-03 08:23
It's actually better to close the socket in __init__ in this case.
msg219690 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-06-03 14:25
New changeset d5c76646168d by Senthil Kumaran in branch '3.4':
Fix issue #21641: Close the socket before raising the SMTPResponseException. Fixes the ResourceWarning in the test run.
http://hg.python.org/cpython/rev/d5c76646168d

New changeset 7ea84a25d863 by Senthil Kumaran in branch 'default':
merge from 3.4
http://hg.python.org/cpython/rev/7ea84a25d863
msg219691 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2014-06-03 14:26
The first patch was correct and consistent with how other Exceptions were closing the socket. Fixed this and thanks for the patch.
History
Date User Action Args
2014-06-03 14:26:53orsenthilsetstatus: open -> closed

assignee: orsenthil
versions: + Python 3.4
nosy: + orsenthil

messages: + msg219691
resolution: fixed
stage: resolved
2014-06-03 14:25:48python-devsetnosy: + python-dev
messages: + msg219690
2014-06-03 08:23:48Claudiu.Popasetfiles: + issue21641.patch

messages: + msg219669
2014-06-02 14:57:54Claudiu.Popasetmessages: + msg219593
2014-06-02 14:55:42Claudiu.Popacreate