Skip to content

Commit 0cff49f

Browse files
committed
#22215: have the smtplib 'quit' command reset the state.
Without this reset, starttls would fail if a connect/starttls was done after a quit, because smtplib assumed the existing value of emspt_features was accurate, but it gets reset when starttls completes (and the new value does not contain the starttls capability, since tls is already started at that point). (There may be additional places where this lack of reset was an issue as well.) Patch by Milan Oberkirch.
1 parent a64b92e commit 0cff49f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/smtplib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,10 @@ def close(self):
866866
def quit(self):
867867
"""Terminate the SMTP session."""
868868
res = self.docmd("quit")
869+
# A new EHLO is required after reconnecting with connect()
870+
self.ehlo_resp = self.helo_resp = None
871+
self.esmtp_features = {}
872+
self.does_esmtp = False
869873
self.close()
870874
return res
871875

Lib/test/test_smtplib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,21 @@ def testAUTH_multiple(self):
858858
self.assertIn(sim_auth_login_password, str(err))
859859
smtp.close()
860860

861+
def test_quit_resets_greeting(self):
862+
smtp = smtplib.SMTP(HOST, self.port,
863+
local_hostname='localhost',
864+
timeout=15)
865+
code, message = smtp.ehlo()
866+
self.assertEqual(code, 250)
867+
self.assertIn('size', smtp.esmtp_features)
868+
smtp.quit()
869+
self.assertNotIn('size', smtp.esmtp_features)
870+
smtp.connect(HOST, self.port)
871+
self.assertNotIn('size', smtp.esmtp_features)
872+
smtp.ehlo_or_helo_if_needed()
873+
self.assertIn('size', smtp.esmtp_features)
874+
smtp.quit()
875+
861876
def test_with_statement(self):
862877
with smtplib.SMTP(HOST, self.port) as smtp:
863878
code, message = smtp.noop()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #22216: smtplib now resets its state more completely after a quit. The
31+
most obvious consequence of the previous behavior was a STARTTLS failure
32+
during a connect/starttls/quit/connect/starttls sequence.
33+
3034
- Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait()
3135
caused by mutation of the waiters queue without holding the lock. Patch
3236
by Doug Zongker.

0 commit comments

Comments
 (0)