Skip to content

Explicitly set accepted socket to blocking #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2022

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Oct 25, 2022

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

@jepler jepler requested a review from a team October 25, 2022 15:11
@anecdata
Copy link
Member

Explicitly set accepted socket blocking?

Copy link
Member

@tekktrik tekktrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on PiCow and regression-tested on ESP32-S2. Vast improvement on PiCow.

But I think the PR title is backwards.

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
@jepler jepler force-pushed the explicit-blocking branch from 73f87c4 to cee7f22 Compare October 25, 2022 16:17
@jepler jepler changed the title Explicitly set accepted socket non-blocking Explicitly set accepted socket to blocking Oct 25, 2022
@jepler
Copy link
Contributor Author

jepler commented Oct 25, 2022

I changed the title and the commit message, thanks!

@paul-1
Copy link
Contributor

paul-1 commented Oct 26, 2022

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?

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 26, 2022
@domi-a
Copy link

domi-a commented Oct 26, 2022

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
Copy link
Contributor

paul-1 commented Oct 26, 2022

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.

length, _ = conn.recvfrom_into(self._buffer)

I'll move this to a new issue.

@domi-a
Copy link

domi-a commented Oct 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no response to http requests on pico w /CP8 Beta non-blocking tcp sockets not working on pico w
5 participants