Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.x] Fixed #19134 -- Allowed closing smtp backend when the server …

…is stopped

Thanks Sebastian Noack for the report and the initial patch.
Backport of 1b3f832 from master.
  • Loading branch information...
commit 4081042ef51c0e679c78f2d0d13514af3434ed13 1 parent a893ee3
@claudep claudep authored
View
5 django/core/mail/backends/smtp.py
@@ -63,9 +63,10 @@ def close(self):
try:
try:
self.connection.quit()
- except socket.sslerror:
+ except (socket.sslerror, smtplib.SMTPServerDisconnected):
# This happens when calling quit() on a TLS connection
- # sometimes.
+ # sometimes, or when the connection was already disconnected
+ # by the server.
self.connection.close()
except:
if self.fail_silently:
View
19 tests/regressiontests/mail/tests.py
@@ -659,9 +659,9 @@ def run(self):
asyncore.close_all()
def stop(self):
- assert self.active
- self.active = False
- self.join()
+ if self.active:
+ self.active = False
+ self.join()
class SMTPBackendTests(BaseEmailBackendTests, TestCase):
@@ -715,3 +715,16 @@ def test_email_disabled_authentication(self):
backend = smtp.EmailBackend(username='', password='')
self.assertEqual(backend.username, '')
self.assertEqual(backend.password, '')
+
+ def test_server_stopped(self):
+ """
+ Test that closing the backend while the SMTP server is stopped doesn't
+ raise an exception.
+ """
+ backend = smtp.EmailBackend(username='', password='')
+ backend.open()
+ self.server.stop()
+ try:
+ backend.close()
+ except Exception as e:
+ self.fail("close() unexpectedly raised an exception: %s" % e)

0 comments on commit 4081042

Please sign in to comment.
Something went wrong with that request. Please try again.