Code

Opened 6 years ago

Closed 6 years ago

#6835 closed (fixed)

django.core.mail.SMTPConnection.open() method performs an unnecessary call to socket.getfqdn()

Reported by: George Murdocca <gmurdocca@…> Owned by: PhiR
Component: Core (Mail) Version: master
Severity: Keywords: local_hostname, smtplib, CachedDnsName, SMTPConnection
Cc: gmurdocca@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Summary

This is an optimization fix as opposed to a bug. The django.core.mail.SMTPConnection.open() method performs an extraneous call to socket.getfqdn() which affects performance. The mail.py module has a class defined called CachedDnsName() whos get_fqdn() method addresses this issue by calling socket.getfqdn(), and caches the result, thus removing the need to call it again later. The fix is extremely trivial to implement. Here are the details:

Detailed Description

On line 122 of the mail.py module, an smtplib.SMTP() object is instantiated:

self.connection = smtplib.SMTP(self.host, self.port)

The constructor of smtplib.SMTP() accepts three arguments, namely host='', port=0, local_hostname=None. However, local_hostname is omitted in mail.py. When omitted, socket.getfqdn() is called, and its value is assigned to the missing argument. This call is extraneous and slow, because:

  • socket.getfqdn() is slow to evaluate (as mentioned in the comment at line 28 of mail.py)
  • The result of socket.getfqdn() already done once and cached by django.core.mail.CachedDnsName.get_fqdn(), and shoul dbe used instead.

The Fix

Change line 122 of mail.py to:

self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())

The Diff

--- mail.py     2008-03-02 12:45:20.000000000 +1100
+++ mailjrj.py  2008-03-20 14:04:07.000000000 +1100
@@ -119,7 +119,7 @@
             # Nothing to do if the connection is already open.
             return False
         try:
-            self.connection = smtplib.SMTP(self.host, self.port)
+            self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
             if self.use_tls:
                 self.connection.ehlo()
                 self.connection.starttls()

Cheers,
George Murdocca & Phil Wright.

Attachments (1)

6835.diff (580 bytes) - added by PhiR 6 years ago.
proper patch against SVN

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by PhiR

proper patch against SVN

comment:1 Changed 6 years ago by PhiR

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to PhiR
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

applies to SVN, and successfully tested on my app.

comment:2 Changed 6 years ago by gwilson

  • Resolution set to fixed
  • Status changed from new to closed

(In [7348]) Fixed #6835 -- Use cached FQDN when creating smtplib.SMTP() connection to avoid a lengthy
socket.getfqdn() call, thanks George Murdocca and PhiR.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.