Opened 10 years ago

Closed 10 years ago

#6835 closed (fixed) method performs an unnecessary call to socket.getfqdn()

Reported by: George Murdocca <gmurdocca@…> Owned by: Philippe Raoult
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:



This is an optimization fix as opposed to a bug. The method performs an extraneous call to socket.getfqdn() which affects performance. The 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 module, an smtplib.SMTP() object is instantiated:

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

The constructor of smtplib.SMTP() accepts three arguments, namely host='', port=0, local_hostname=None. However, local_hostname is omitted in 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
  • 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 to:

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

The Diff

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

George Murdocca & Phil Wright.

Attachments (1)

6835.diff (580 bytes) - added by Philippe Raoult 10 years ago.
proper patch against SVN

Download all attachments as: .zip

Change History (3)

Changed 10 years ago by Philippe Raoult

Attachment: 6835.diff added

proper patch against SVN

comment:1 Changed 10 years ago by Philippe Raoult

Has patch: set
Owner: changed from nobody to Philippe Raoult
Triage Stage: UnreviewedReady for checkin

applies to SVN, and successfully tested on my app.

comment:2 Changed 10 years ago by Gary Wilson

Resolution: fixed
Status: newclosed

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

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