uWSGI raises OSError: write error during large request

uWSGI raises OSError: write error during large request

我的应用程序使用 nginx,在服务器端使用 uWSGI。当我做一个大请求(响应时间> 4s)时,出现以下内容:

SIGPIPE: writing to a closed pipe/socket/fd (probably the client
    disconnected) on request _URL_ (ip XX.XX.XX.XX) !!!

uwsgi_response_writev_headers_and_body_do(): Broken pipe
    [core/writer.c line 287] during GET _URL_ (XX.XX.XX.XX)

OSError: write error

似乎 uWSGI 试图写入一个流,但是这个流已经被关闭了。 当我检查 nginx 日志时 (error.log):

upstream prematurely closed connection while reading response
    header from upstream ...

当然,我的客户端(REST 客户端或浏览器)收到 502 错误。

我总是在 ~4 秒后收到此错误。

但是,我不知道如何防止这个问题。 我试图在我的 nginx 配置文件中设置一些参数:

location my_api_url {
    [...]
    uwsgi_buffer_size 32k;
    uwsgi_buffers 8 32k;
    uwsgi_busy_buffers_size 32k;

    uwsgi_read_timeout 300;
    uwsgi_send_timeout 300;

    uwsgi_connect_timeout 60;
}

但问题依然存在。 我还尝试在 uWSGI 配置文件中设置这些参数 (wsgi.ini):

buffer-size=8192
ignore-sigpipe=true
ignore-write-errors=true

在尝试优化响应时间之前,我希望这个问题有解决方案。我没有找到一个在另一个 post 中工作的。我处理大量数据,所以我的响应时间在某些情况下会在 4-10 秒之间。

希望你能帮助我:)

非常感谢。

可能是你上传东西的时候使用了分块编码。 有 uWSGI 选项 --chunked-input-timeout, 默认情况下是 4 秒(defaults--socket-timeout 的值,即 4 秒)。

虽然理论上问题可能出在其他地方,但我建议您尝试 上述选项。另外,烦人的异常是我有

的原因
ignore-sigpipe=true
ignore-write-errors=true
disable-write-exception=true

在我的 uWSGI 配置中(注意我提供了 3 个选项,而不是 2 个):

  • ignore-sigpipe 使 uWSGI 不显示 SIGPIPE 错误;
  • ignore-write-errors 使其不显示错误 例如uwsgi_response_writev_headers_and_body_do;
  • disable-write-exception 防止 OSError 写入生成。

在我的例子中,Nginx 作为 uwsgi 的反向代理,配置 http-timeout 将服务器设置为正常等待 运行 长请求。

注意 nginx 代理声明中包含以下选项:

proxy_read_timeout 300s;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;

关于网关超时什么都不做

我在 uwsgi.ini 配置中添加了 [socked-timeout=xxx],它起作用了。