title: Python:SMTP init method hangs when used in try:except block
body: <p>I am trying to send some backtraces via email for which i am using the python <code>SMTP</code> lib.</p>
  
  <p>My code looks like this</p>
  
  <pre><code>def send_email_thread(from_addr, to_addr, message):
      server = smtplib.SMTP("smtp.googlemail.com", 587)
      server.ehlo()
      server.starttls()
      server.login(SENDMAIL_SMTP_USER, SENDMAIL_SMTP_PASSWD)
      server.sendmail(from_addr, to_addr, message)
      server.quit()
  </code></pre>
  
  <p>The actual code where i am trying to get the traceback</p>
  
  <pre><code>try:
      /*Something here that gives exception*/
  except Exception:
      send_email_thread(from_addr,to_addr, str(traceback.format_exc()))
  </code></pre>
  
  <p>Now the problem is that the line</p>
  
  <pre><code>server = smtplib.SMTP("smtp.googlemail.com", 587)
  </code></pre>
  
  <p>hangs and does not return. On digging the issue a little i found that <code>__init__</code> for <code>SMTP</code> class calls <code>connect()</code> which in turn calls socket function <code>createconnection</code> which uses linux's <code>getaddrinfo()</code></p>
  
  <p>Call path:</p>
  
  <pre><code>mycode--&gt;smtplib.SMTP---&gt; __init__---&gt;connect----&gt;createconnection()---&gt;getaddrinfo()
  </code></pre>
  
  <p>The <code>getaddrinfo()</code> call never returns. This is the hang point. What could be the problem? Also, I don't receive any traceback  or exception from <code>SMTP</code> lib. Is that because I am already catching an exception?</p>
  
  <p>This only happens when i call the <code>send_mail_thread()</code> function inside the <code>except</code> block. From above:</p>
  
  <pre><code>except Exception:
      send_email_thread(from_addr,to_addr, str(traceback.format_exc()))
  </code></pre>
  
  <p>I tried by placing it outside and it worked! Which signals that the catching the exception and then running this might have caused the issue? Any pointers? I am using python 2.7 on Linux</p>
  
  <p>[edit]</p>
  
  <p>After some of the comments below i wanted to add the following update. The code runs inside a process created from another main process(using <code>multiprocessing</code> module). The <code>send_email_function</code> is then called inside a thread of this child process.</p>
  
  <p>I created a simpler program to test this in which everything was being done in one process. And it worked out. Which means that there is some problem(deadlock) with how <code>getaddrinfo()</code> behaves when there is an exception in some child process or how well the <code>multiprocessing</code> modules handles POSIX calls</p>