Opened 6 years ago

Closed 6 years ago

#19382 closed Bug (fixed)

Closing SMTP backend raises AttributeError when already closed

Reported by: Sebastian Noack Owned by: nobody
Component: Core (Mail) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no


>>> from django.core.mail import get_connection
>>> smtp = get_connection('django.core.mail.backends.smtp.EmailBackend')
>>> smtp.close()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/sebastian/p24/src/placement24/placement24/django/core/mail/backends/", line 66, in close
AttributeError: 'NoneType' object has no attribute 'quit'

The SMTP backend is the only mail backend that can't be closed without opening it. In fact the filebased backend contains code to explicitly handle that case. So the smtp backend should handle it as well.

If that is not reason enough, note that it is not always possible to know whether the connection is open and must be closed or not. For example, when open() was called and an SMTP connection was established, but the login failed, close() must be called even though open() raised an exception. However when the exception was raised while establishing the connection, close() would raise the AttributeError, shown above.

Therefore the close() method must just return in case the connection isn't open.

Attachments (1)

0001-Don-t-fail-when-closing-closed-smtp-backend.patch (779 bytes) - added by Sebastian Noack 6 years ago.

Download all attachments as: .zip

Change History (5)

Changed 6 years ago by Sebastian Noack

comment:1 Changed 6 years ago by atykhonov

Easy pickings: set
Triage Stage: UnreviewedReady for checkin

comment:2 Changed 6 years ago by Claude Paroz

Needs tests: set
Triage Stage: Ready for checkinAccepted
Type: UncategorizedBug

Tests are needed.

comment:3 Changed 6 years ago by Claude Paroz

This is very close to #19134. There, I've given a clue about how to test it. Might be even worth fixing both tickets together.

comment:4 Changed 6 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In 5b8c0d22cc07ca727c032edf3309fab9cb39f21d:

[1.5.x] Fixed #19382 -- Stopped smtp backend raising exception when already closed

Thanks Sebastian Noack for the report and the initial patch.
Backport of ffa50ca35 from master.

Note: See TracTickets for help on using tickets.
Back to Top