socket - ERR_CONNECTION_RESET on 4th connection

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Wed Nov 07, 2018 9:24 am

I know it's not much to go on, but I have a simple webserver that I have not used in probably 6 months.

I was just testing it with the latest build, and now I am getting "ERR_CONNECTION_RESET" on the 4th connection, even though I have connections set to 5.

Previously the web browser would wait for available connection, but now it appears that the socket is actively resetting connections.

Before I start rolling back to previous micropythion versions to see if I can find the cause, has anyone else experienced any problems with multiple socket connections on the esp8266 ??
Last edited by devnull on Thu Nov 08, 2018 4:20 am, edited 1 time in total.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET

Post by devnull » Wed Nov 07, 2018 11:42 pm

Spent a day on this, I have tried going back to 1.93 and 1.92 and the results are identical.

It appears that the socket is only accepting 3 connections, and any requests that exceed 3 that are made by chrome (or firefox or safari) browser are dropped by the socket rather than queued, despite the fact that listen() is set for 5 connections which is the max.

I have also tried reducing this to 4,3,2 and 1 but the behavior is identical.

In the html page, there are 4 files (5 including the actual index.htm page) that are loaded in the header:

Code: Select all

<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" type="text/css" href="css/tabs.css">
<link rel="stylesheet" type="text/css" href="css/xtabs.css">
<script type="text/javascript" src="js/index.js"></script>
Only index.htm, index.css and tabs.css ever get loaded, chrome reports that xtabs.css and index.js as:

Code: Select all

net::ERR_CONNECTION_RESET
I have logged the reading of each line immediatly after accept(), and it only ever shows the first 3 connections, there is no sign of the request for the other files ever reaching the accept():

HTTP serve on IP: 192.168.0.113 PORT: 80

Code: Select all

b'Host: 192.168.0.113\r\n'
b'Connection: keep-alive\r\n'
b'Cache-Control: max-age=0\r\n'
b'Upgrade-Insecure-Requests: 1\r\n'
b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36\r\n'
b'Sec-Metadata: cause="forced", destination="document", site="cross-site"\r\n'
b'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n'
b'Accept-Encoding: gzip, deflate\r\n'
b'Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,pl;q=0.7,la;q=0.6\r\n'
b'\r\n'

b'Host: 192.168.0.113\r\n'
b'Connection: keep-alive\r\n'
b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36\r\n'
b'Sec-Metadata: destination=style, site=same-origin\r\n'
b'Accept: text/css,*/*;q=0.1\r\n'
b'Referer: http://192.168.0.113/\r\n'
b'Accept-Encoding: gzip, deflate\r\n'
b'Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,pl;q=0.7,la;q=0.6\r\n'
b'\r\n'

b'Host: 192.168.0.113\r\n'
b'Connection: keep-alive\r\n'
b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36\r\n'
b'Sec-Metadata: destination=style, site=same-origin\r\n'
b'Accept: text/css,*/*;q=0.1\r\n'
b'Referer: http://192.168.0.113/\r\n'
b'Accept-Encoding: gzip, deflate\r\n'
b'Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,pl;q=0.7,la;q=0.6\r\n'
b'\r\n'
And this is the main code that handles the requests:

Code: Select all

    
    self.optimise = True
    while True:
      print('HTTP serve on IP:', wlan.net.ifconfig()[0],'PORT:', self.port )
      self.sock = socket.socket()
      ai = socket.getaddrinfo("0.0.0.0", self.port)
      addr = ai[0][-1]
  
      self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      self.sock.bind(addr)
      self.sock.listen(5)
  
      while True:
        bits = self.sock.accept()
        req = {
          "sock": bits[0],
          "addr": bits[1],
          "head": [],
          "path": None
        }
        
        if not self.optimise:
          res = req['sock'].makefile("rwb")
        else:
          res = req['sock']
  
        req['get'] = res.readline().decode('UTF-8').rstrip();
        
        while True:
          h = res.readline()
          print(h)
          if h == b"" or h == b"\r\n":
            break
          else:
            req['head'].append(h.decode('UTF-8').rstrip())
        
        self.request(req,res)
        if not self.optimise:
          req['sock'].close()
        
        req = None
        res = None
        gc.collect()

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Thu Nov 08, 2018 7:40 am

As you probably know, web browsers open multiple connections to a web server when the page includes other files such as css style sheets, javascript, images etc etc.

I am unable to successfully load all of the requested files and would appreciate if someone could also test this ??

https://github.com/pacmac/micropython-s ... master/www

Just copy the files to your esp8266 including the htm folder and it's contents and then run the web server:

Code: Select all

import wwwtest
wwwtest.WWW().serve()
And when the device is connected to your wlan, open up your web browser to the device IP address i.e.:

Code: Select all

http://192.168.0.100/
The index.htm attempts to load 5 style sheets, red.css, orange.css, yellow.css, green.css and blue.css.

If all files get loaded then the page should display all 5 colours, when I test it on my esp8266, I only ever get maximum 3 colours and the web server logs only ever show all attempted browser connections, even though the server made 6 requests, one for the index.htm page and one for each style sheet.

As far as I understand, the socket should queue up the requests (5) but it appears that not all of the requests are reaching the socket.accept() ?

If you are using chrome, you can enable developer tools, and then you can see the failed network requests and the console error messages.

Really appreciate if someone can independently verify this is happening !

Thanks so much

Enable Developer Tools in Chrome

Click the 3 vertical dots in the top right hand side of chrome, then select "More Tools" > "Developer Tools" and then select the 'Network" or "Console" tabs before refreshing the page.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Thu Nov 08, 2018 9:55 am

When I run the same test on the ESP32 there is no problem and all of the css file includes load every time.

So it looks like the esp8266 'may' have a bug with the socket module ?

Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by pythoncoder » Thu Nov 08, 2018 10:20 am

In Firefox the text "Green" and "Blue" show black here (on the ESP8266 reference board) :(

[EDIT]
I spoke too soon. I closed the browser tab, went into history and re-opened it: now all but "Yellow" are displaying correctly. The server output is

Code: Select all

MicroPython v1.9.4-684-g1dd4d93-dirty on 2018-11-06; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import wwwtest
=== wwwtest.WWW().serve()
=== 
1 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33216))
2 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33218))
3 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33220))
4 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33222))
5 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33270))
6 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33274))
7 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33276))
8 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33278))
9 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33292))
Peter Hinch
Index to my micropython libraries.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Thu Nov 08, 2018 10:25 am

Peter, thanks so much for helping, yes that means that those 2 css files were not loaded.

in firefox you can see the errors using the Developer Console:

1) Click the icon with the 3 vertical lines in the top right of the firefox browser
2) Select 'Web Developer" > "Web Console" and the console will be displayed in the bottom part of the screen.
3) Select either the Network or Console tab and then refresh the page, you will see the errors.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Thu Nov 08, 2018 10:27 am

If you get it from history then the browser is probably caching the previous request, use the reload button in the address bar.

But you should get all of the colours, run the same test on esp32 and you will get every colour every time.

Sometimes the browser will cache previously loaded files, so you always need to reload the page.

Unless all colours are shown, one of the files has not been loaded.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by pythoncoder » Thu Nov 08, 2018 10:33 am

I do. I'm afraid I can't offer much advice here - I don't really know how these things work at the socket level.

The behaviour is erratic - I have seen it display correctly. I also got this after a while:

Code: Select all

21 (<socket state=2 timeout=-1 incoming=0 off=0>, ('192.168.0.35', 33378))
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "wwwtest.py", line 56, in serve
  File "wwwtest.py", line 27, in request
ValueError: need more than 1 values to unpack
>>> 
Peter Hinch
Index to my micropython libraries.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by devnull » Thu Nov 08, 2018 10:39 am

Yes, that error is because I am not doing any error checking on the received request, I just created this bare server just to prove / disprove this problem.

That particular error is caused because a received line was null or badly formatted, in my live server all of this is 'handled'

Code: Select all

      get = res.readline()
      if get:
        bits = get.decode('UTF-8').rstrip().split(' ')
        if len(bits) == 3: #['GET', '/', 'HTTP/1.1']
          req['method'], req['path'],req['ver'] = bits
          if req['path'] == '/': req['path'] = self.home 
If you try this on esp32 you should always get all the colours.

Thanks again, I have also reported this on an existing issue thread on github.

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: socket - ERR_CONNECTION_RESET on 4th connection

Post by nagylzs » Sat Jan 19, 2019 8:06 pm

I ran into the very same problem! I have been struggling with this since days. In my case, the micropython web server was working when it was viewed from chrome on android. I have installed chrome remote debugging tools, and it turned out that there are net::ERR_CONNECTION_RESET errors in the console. The network tab shows these requests as "failed". All requests are logged on the micropython web server right after socket.accept(), and the failed requests are not logged - e.g. they are not accepted by the server socket. I'm also using ESP8266. I wonder if there is any solution for this problem already?

Post Reply