Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Claude Paroz authored January 03, 2013
5  django/core/mail/backends/smtp.py
@@ -63,9 +63,10 @@ def close(self):
63 63
         try:
64 64
             try:
65 65
                 self.connection.quit()
66  
-            except socket.sslerror:
  66
+            except (socket.sslerror, smtplib.SMTPServerDisconnected):
67 67
                 # This happens when calling quit() on a TLS connection
68  
-                # sometimes.
  68
+                # sometimes, or when the connection was already disconnected
  69
+                # by the server.
69 70
                 self.connection.close()
70 71
             except:
71 72
                 if self.fail_silently:
19  tests/regressiontests/mail/tests.py
@@ -659,9 +659,9 @@ def run(self):
659 659
         asyncore.close_all()
660 660
 
661 661
     def stop(self):
662  
-        assert self.active
663  
-        self.active = False
664  
-        self.join()
  662
+        if self.active:
  663
+            self.active = False
  664
+            self.join()
665 665
 
666 666
 
667 667
 class SMTPBackendTests(BaseEmailBackendTests, TestCase):
@@ -715,3 +715,16 @@ def test_email_disabled_authentication(self):
715 715
         backend = smtp.EmailBackend(username='', password='')
716 716
         self.assertEqual(backend.username, '')
717 717
         self.assertEqual(backend.password, '')
  718
+
  719
+    def test_server_stopped(self):
  720
+        """
  721
+        Test that closing the backend while the SMTP server is stopped doesn't
  722
+        raise an exception.
  723
+        """
  724
+        backend = smtp.EmailBackend(username='', password='')
  725
+        backend.open()
  726
+        self.server.stop()
  727
+        try:
  728
+            backend.close()
  729
+        except Exception as e:
  730
+            self.fail("close() unexpectedly raised an exception: %s" % e)

0 notes on commit 4081042

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