adafruit / Adafruit_CircuitPython_HTTPServer

Simple HTTP Server for CircuitPython
MIT License
46 stars 30 forks source link

Explicitly set accepted socket to blocking #19

Closed jepler closed 2 years ago

jepler commented 2 years ago

The Python documentation states that otherwise, the blocking status of the newly accepted socket is implementation-defined:

if the listening socket is in non-blocking mode, whether the socket returned by accept() is in blocking or non-blocking mode is operating system-dependent. If you want to ensure cross-platform behaviour, it is recommended you manually override this setting. https://docs.python.org/3/library/socket.html#socket-timeouts

When the connected socket is non-blocking (as it is on picow), the http library works erratically, depending whether the request has already arrived by the time recvfrom_into call occurs.

Closes: adafruit/circuitpython#7086

anecdata commented 2 years ago

Explicitly set accepted socket blocking?

jepler commented 2 years ago

I changed the title and the commit message, thanks!

paul-1 commented 2 years ago

How is this intended to work with a httpserver using poll() in a loop?

Other tasks in the program function properly, calling poll every loop. The first time a connection is made to the httpserver, then the connection transitions to blocking, then everything else in the program stops working indefinitely.

When explicitly transitioning to blocking mode, would it make sense to optionally set a timeout to allow things to transition back to non-blocking?

domi-a commented 2 years ago

How is this intended to work with a httpserver using poll() in a loop?

it does now work with poll() in the loop. and does not block other things while waiting to requests. this was not possible before this fix.

testet with CP 8 beta 3 + this very change on pico-w.

paul-1 commented 2 years ago

Yes, same upgrade to Circuitpython and httpserver on a picoW. What browser are you using?

Firefox: Everything works fine. Chrome: Socket/connection is left in a blocking state. But the block actually comes after the web page is loaded, it appears that chrome is reopening a connection in anticipation of the next request.

It is actually this call that is blocking, when there are no bytes to receive. https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/f9345f2d694838604a54bda3468d66fd5e4abb68/adafruit_httpserver.py#L346

I'll move this to a new issue.

domi-a commented 2 years ago

Yes, same upgrade to Circuitpython and httpserver on a picoW. What browser are you using?

Firefox: Everything works fine.

i used the lastest release that containes the fix https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/releases/tag/0.5.3 and it workes in FF and in chrome and it can load the page as often i want including a asset that is loaded from the document. all while code in loop is doing even other stuff