Forums

Network (un)available when sending email

Hi guys,

My web app sends email to a user every time she creates some entity (registers a eam for a competition). From time to time (like 20% of cases) sending email from PY fails resulting in Exception and Server Error (500).

There has been 30 entities created (teams registered) over 48 hours, so it's not a high load. This piece of log shows that the 1st attempt failed and in two minutes the 2nd attempt went OK. On my local dev server I never had this error.

Why could this happen?

2018-07-01 12:47:43,912 [uWSGIWork] [INFO ] [competition.views.team  ]  Sending email:Регистрация команды "Следопуты" на "Московский Лайт 2018"
2018-07-01 12:47:45,923 [uWSGIWork] [ERROR] [django.request          ]  Internal Server Error: /comp/team/2/2/
Traceback (most recent call last):
  File "/home/regrace/.virtualenvs/sport_event/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/regrace/.virtualenvs/sport_event/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/regrace/.virtualenvs/sport_event/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/regrace/.virtualenvs/sport_event/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/regrace/.virtualenvs/sport_event/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/regrace/_git/sport_event/competition/views/team.py", line 289, in post
    EmailThread(to=res['emails'], subj=subj, msg=msg).run()   # test if it would work within the main thread
  File "/home/regrace/_git/sport_event/competition/views/e_mail.py", line 37, in run
    server = smtplib.SMTP(self.server_name, self.port)
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 101] Network is unreachable
2018-07-01 12:49:42,483 [uWSGIWork] [INFO ] [competition.views.team  ]  Registered the team 'title':'Следопуты', 'num.pers.':'2', 'num.maps.':'2', 'klass':'Московский Лайт 2018:Общий', 'number':'28', 'submitte
d_at':'2018-07-01 09:49:42.444805+00:00', 'paid_at':'None', 'paid':'0', 'is_paid':'False'
2018-07-01 12:49:42,494 [uWSGIWork] [INFO ] [competition.views.team  ]  Sending email:Регистрация команды "Следопуты" на "Московский Лайт 2018"
2018-07-01 12:49:42,601 [uWSGIWork] [ERROR] [root                    ]  send: 'ehlo [127.0.0.1]\r\n'
2018-07-01 12:49:42,616 [uWSGIWork] [ERROR] [root                    ]  reply: '250-smtp.gmail.com at your service, [23.21.186.62]\r\n'

There may be some connection sharing in your app and long-running connections get closed by the server. In that case, you would need to rebuild the connection. The reason you may not be seeing it locally could then be because of how long the app has been running.