0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
abidibo
  • 4,175
  • 2
  • 25
  • 33
  • 1
    Is something going wrong with your server-side code, causing it to loop and create far more files than intended? That might explain the multiple emails and 502 error, which is a server timeout when a request doesn't complete (if it keeps on trying to create files with no limit). Or alternatively, does the backend code use other services such as mysql or redis and there is an issue connecting to them? By no means an expert on uwsgi/nginx, but sharing your view code might shed some light on it. – birophilo Feb 01 '19 at 23:44
  • @birophilo thanks, I've updated my question showing an extract of my view code. I'm quite sure there's no a loop in my server side code, otherwise the problem should always happen. And yes, I use mysql to store data, but when the db is unreachable django exits with an operation error, and I receive an email with the stack trace, and that didn't happened – abidibo Feb 02 '19 at 09:33

0 Answers0