Does not handle upstream closing the connection
See original GitHub issueSteps to reproduce the problem:
- Use a setup such as redirect_requests.py
- Send a keepalive request into MITM
- This is passed onto the upstream server and response returned (fine)
- Wait a little while - upstream closes the connection with a FIN
- (MITMproxy did not respond to this)
- Wait a bit longer and send another request to MITM
- MITM attempts to send this to the upstream which responds with a RST
- Client sees a 502 bad gateway error
Any other comments? What have you tried so far?
In our case we were using https://www.npmjs.com/package/json-server#cli-usage as the upstream server but it appears to be complying with keepalive docs. Just very surprised we are the only people seeing this 😃
Workaround for us was to add flow.request.headers[‘Connection’] = ‘close’, this closed the upstream connection after each request.
GET /config HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 7.1.1; Android SDK built for x86 Build/NYC) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: identity
Accept-Language: en-US,en;q=0.8
HTTP/1.1 200 OK
X-Powered-By: Express
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Content-Length: 4094
ETag: W/"ffe-YXiFVkdRXmM5eFcp/fDfl4O1X+w"
Date: Wed, 04 Apr 2018 12:53:09 GMT
Connection: keep-alive
{
"content": {
....
}
[ NB: FIN is sent by upstream and received by MITM here]
GET /config HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 7.1.1; Android SDK built for x86 Build/NYC) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: identity
Accept-Language: en-US,en;q=0.8
If-None-Match: W/"ffe-YXiFVkdRXmM5eFcp/fDfl4O1X+w"
[ RST now received]
System information
Mitmproxy: 3.0.3 Python: 3.6.5 OpenSSL: OpenSSL 1.0.2o 27 Mar 2018 Platform: Darwin-17.5.0-x86_64-i386-64bit
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Upstream Closing Connection Causes Next Request to Fail
It's HTTP/1.1. When I was running non-TLS (port 80) it was sending that correctly based on examining the packets. Now that I switched...
Read more >How to solve the "upstream prematurely closed connection ...
This error means the upstream, in this case Artifactory, closed the connection. Problem Resolution. Artifactory's Apache Tomcat has a hidden ...
Read more >nginx reverse proxy - doesn't close upstream connection when ...
Everything works fine over HTTP, but as soon as I turn SSL on, nginx seems to hold the upstream connection open for a...
Read more >Upstream closing down connections for uwsgi, Flask and ...
Both nginx and uwsgi try to bind port 80, so try to change uwsgi's port to something different value or just delete the...
Read more >Avoiding the Top 10 NGINX Configuration Mistakes
Note that the keepalive directive does not limit the total number of connections to upstream servers that an NGINX worker process can open ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We just released mitmproxy 3.0.4, which includes a whole bunch of bugfixes but most importantly should fix this issue. Thanks again for reporting this, everyone! 😃
Thanks again for the great report. There have been quite many reports regarding 502 errors, and e.g. @alekseynp helped a lot in #3003. I wasn’t sure on the root cause for now though, your report helped a lot. If everything goes as planned, we’ll ship a 3.0.4 bugfix release in an hour.