I am trying to send some backtraces via email for which i am using the python SMTP
lib.
My code looks like this
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()
The actual code where i am trying to get the traceback
try:
/*Something here that gives exception*/
except Exception:
send_email_thread(from_addr,to_addr, str(traceback.format_exc()))
Now the problem is that the line
server = smtplib.SMTP("smtp.googlemail.com", 587)
hangs and does not return. On digging the issue a little i found that __init__
for SMTP
class calls connect()
which in turn calls socket function createconnection
which uses linux's getaddrinfo()
Call path:
mycode-->smtplib.SMTP---> __init__--->connect---->createconnection()--->getaddrinfo()
The getaddrinfo()
call never returns. This is the hang point. What could be the problem? Also, I don't receive any traceback or exception from SMTP
lib. Is that because I am already catching an exception?
This only happens when i call the send_mail_thread()
function inside the except
block. From above:
except Exception:
send_email_thread(from_addr,to_addr, str(traceback.format_exc()))
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
[edit]
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 multiprocessing
module). The send_email_function
is then called inside a thread of this child process.
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 getaddrinfo()
behaves when there is an exception in some child process or how well the multiprocessing
modules handles POSIX calls