Gunicorn退出,因为来自Django的https请求在OpenSSL中失败

时间:2014-09-16 23:34:38

标签: django openssl signals gunicorn

这是场景。我有一个Django应用程序由Gunicorn在Linux上提供服务。在某些请求中,它通过httplib2.request()对外部API进行https调用。有时这个调用失败的方式是软管OpenSSL(不确定如何,但这不是我的错,并不重要)。在这种情况下,OpenSSL向gunicorn发送SIGABRT信号。 Gunicorn处理SIGABRT并迅速系统退出(应该如此)。

我发现根本问题是OpenSSL异步地通知父进程中止,而不是返回错误代码。不要因为你的个人问题而告诉我中止,OpenSSL!传统代码在行动中。

是否还有其他人对此问题彻底恼火?你怎么能阻止这个杀死你的Gunicorn进程?它完全绕过了Django异常处理。

相关代码点:

1 个答案:

答案 0 :(得分:1)

我认为信号不是由 OpenSSL 创建的。该信号来自 gunicorn,如果请求时间过长,它会发送此信号。

<块引用>

worker_abort(worker):当一个worker收到SIGABRT信号时调用。

此调用通常在超时时发生。

callable 需要接受一个实例变量来初始化 工作者。

https://docs.gunicorn.org/en/stable/settings.html#worker-abort

我的回溯,如果请求超过 N 秒,我会得到它:

SystemExit: 1
  File "django/core/handlers/wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "django/core/handlers/base.py", line 128, in get_response
    response = self._middleware_chain(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "debug_toolbar/middleware.py", line 48, in __call__
    return self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "whitenoise/middleware.py", line 59, in __call__
    response = self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/utils/deprecation.py", line 114, in __call__
    response = response or self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django_middleware_global_request/middleware.py", line 15, in __call__
    return self.get_response(request)
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/contrib/admin/options.py", line 614, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File "django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/options.py", line 1719, in changelist_view
    response = self.response_action(request, queryset=cl.get_queryset(request))
  File "django/contrib/admin/options.py", line 1402, in response_action
    response = func(self, request, queryset)
  File "lala/admin.py", line 252, in send_invoice_via_mail
    send_invoice_mail_failsafe_and_create_log(invoice)
  File "lala/views/send_invoices_via_mail.py", line 24, in send_invoice_mail_failsafe_and_create_log
    return send_invoice_mail(invoice)
  File "contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "lala/views/send_invoices_via_mail.py", line 39, in send_invoice_mail
    mail.send_mail(
  File "django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "django/core/mail/backends/smtp.py", line 109, in send_messages
    sent = self._send(message)
  File "django/core/mail/backends/smtp.py", line 125, in _send
    self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
  File "smtplib.py", line 865, in sendmail
    (code, resp) = self.mail(from_addr, esmtp_opts)
  File "smtplib.py", line 539, in mail
    return self.getreply()
  File "smtplib.py", line 391, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
  File "gunicorn/workers/base.py", line 201, in handle_abort
    sys.exit(1)