Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uWSGI error hr_instance_read(): Connection reset by peer

I have deployed a flask web application using uWSGI and NGINX as the reverse proxy. The application response time is about 9ms in average but after a batch of requests my workers crashing and I got this log on my uWSGI log:

[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 24488] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 4059] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 46764] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 9951] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 9449] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 51355] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 39565] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 63142] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 7861] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 10165] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 10917] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]
[uwsgi-http key: backend client_addr: 172.17.0.1 client_port: 20392] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]

The uWSGI config:

[uwsgi]
http = 0.0.0.0:7000
wsgi-file = app.py
callable = app
processes = 5
lazy = true
lazy-apps = true
buffer-size = 16384
logto = /root/logs/data-gathering.log

Env:

uWSGI==2.0.17
Flask==0.12.2
like image 856
Vahid Kharazi Avatar asked May 05 '18 12:05

Vahid Kharazi


1 Answers

I had the same issue with uWSGI native HTTP server. With the following configuration:

https = :8443,app.crt,app.key,HIGH,!ca.crt
http-workers = 2
http-buffer-size = 32768

; applications
mount = /=/app.py
mount = /asq=/asq.py

; process managment
master = true
vacuum = true
single-interpreter = true
pidfile = /tmp/uwsgi.pid
enable-threads = true
cpu-affinity = 1
processes = 4
threads = 2
offload-threads = 1

close-on-exec = true
buffer-size = 32768

I always got:

[uwsgi-http key: 192.168.1.200:8443 client_addr: 192.168.1.200 client_port: 20179] hr_instance_read(): Connection reset by peer [plugins/http/http.c line 647]

In my case I was performing POST requests and not consuming the body. So after adding post-buffering = 1 to the configuration everything just started to work fine. Same result without the flag and just reading the POST data.

And from uWSGI's "Things to know":

If an HTTP request has a body (like a POST request generated by a form), you have to read (consume) it in your application. If you do not do this, the communication socket with your webserver may be clobbered. If you are lazy you can use the post-buffering option that will automatically read data for you. For Rack applications this is automatically enabled.

like image 127
Vladimir Poghosyan Avatar answered Nov 01 '22 13:11

Vladimir Poghosyan