smtplib: missing case in handling 421 response in sendmail
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
The sendmail function in smtplib does a good job of handling 421 responses from SMTP servers but is missing one case. This may lead to SMTP client connections not being closed properly.
Background
SMTP servers can send 421 responses whenever they need to shut down the connection (RF 5321 3.8). Clients should take care of closing the connection when receiving a 421.
Details
The sendmail function combines the low-level SMTP commands into a full session for sending some text content via SMTP. The function needs to handle 421 responses, as any single command may return a 421 response. The function already handles almost all cases but is missing one for the call of the data function, which does its own response code checking and may raise an exception.
Test to reproduce
The test was derived from the corresponding rosetta-test SMTP test suite and adapted to the smtplib test infrastructure.
def test_421_during_data_cmd(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
smtp.noop()
self.serv._SMTPchannel.data_response = '421 closing'
with self.assertRaises(smtplib.SMTPDataError):
smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message')
self.assertIsNone(smtp.sock)
self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
CPython versions tested on:
CPython main branch, 3.10, 3.12
Operating systems tested on:
Linux
Linked PRs
- gh-132797
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Inspect Lib/smtplib.py around sendmail's data call and the data response checking described in the issue. Run or adapt test_421_during_data_cmd in the smtplib test infrastructure; done means the 421 case raises SMTPDataError, closes the socket, and leaves rcpt_count at zero.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 30/100