I've a running Django app with uwsgi, served by nginx. The same env I use for many applications (https://github.com/abidibo/cookiecutter-django). I've Postfix configured to receive on localhost and Django configured to send email through it.
I have a view which when receiving a POST request creates some files and sends an e-mail.
What happened is that after processing the POST request, the server returned a 502 page, and multiple e-mail were sent (almost 60 e-mail). It doesn't happen every time, just a few, and I couldn't reproduce the bug in my local environment.
I can't figure out what happened, I mean, if some timeout occurred between Django and uwsgi or postfix itself, then why sending all this e-mail, why the code was executed multiple times?
In the application logs I see that a worker died:
- *** HARAKIRI ON WORKER 4 (pid: 8836, try: 1) ***
- HARAKIRI !!! worker 4 status !!!
- HARAKIRI [core 7] 188.92.61.228 - POST /xxx/xxx/xxx/4/ since 1549027535
- HARAKIRI !!! end of worker 4 status !!!
DAMN ! worker 4 (pid: 8836) died, killed by signal 9 :( trying respawn ...
Respawned uWSGI worker 4 (new pid: 21942)
In the nginx error log I see:
[error] 2565#2565: *19983 upstream prematurely closed connection while reading response header from upstream, client: xx.xx.xx.xx, server: xxx.xxx.com, request: "POST /xxx/xxx/xx/4/ HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi_xxx.sock:", host: "xxx.xxx.com", referrer: "http://xxx.xxx.com/xxx/xxx/xx/4/"
Update
This is an extract of the post view function
def post(self, request, pk):
# stuff...
try:
# stuff...
if request.POST.get('send_mail', False) and request.POST.get(
'destinatari', ''):
send_invoice_mail(fattura, [
e.strip()
for e in request.POST.get('destinatari', '').split(',')
])
messages.add_message(
request, messages.SUCCESS,
'Nota di Credito cliente generata con successo')
messages.add_message(
request, messages.SUCCESS,
'La nota di credito è stata correttamente inserita in prima nota'
)
if request.POST.get('send_mail', False):
messages.add_message(request, messages.SUCCESS,
'E-mail correttamente inviata')
return redirect('/admin/fatture/notacreditouscitacliente/')
except Exception as e:
# stuff...
messages.add_message(request, messages.WARNING, str(e))
return redirect(
'/admin/fatture/notacreditoentratafornitoreprodotti')
How can I debug this?