Skip to content
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

loop() timeout parameter should be absolute #169

Merged
merged 4 commits into from
Jul 8, 2023

Conversation

vladak
Copy link
Contributor

@vladak vladak commented Jun 14, 2023

This changes the way how loop() deals with the timeout parameter. I think the original idea was to wait for the timeout and then return what was received in the mean time, regardless of whether anything was actually received. This means that the loop() will always return after the timeout, i.e. not earlier (assuming no fatal error happened).

Inspired by #68 (comment) and the discussion in PR #168.

@brentru brentru self-requested a review June 16, 2023 16:12
Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

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

This PR correctly addresses the comment:

loop() is blocking because _wait_for_msg() is blocking, which is because _sock_exact_recv() is blocking as currently implemented

@vladak I have some questions:

  1. Do we need to change any example files to reflect the modifications to loop()
  2. Have you tested this on hardware yet?

@FoamyGuy
Copy link
Contributor

I'm doing some testing on adafruit/Adafruit_CircuitPython_ESP32SPI#167 which is aiming to make the esp32spi socket match the CPython socket behavior as close as possible, that led me to this problem and solution proposed here.

@brentru From my testing I think I can answer these:

1. Do we need to change any example files to reflect the modifications to loop()
2. Have you tested this on hardware yet?

1: The examples do not need to change as a result of the change in this PR because there is a default provided for timeout=0 and all of the examples call loop() without passing anything so they'll use this default value, which results in the expected behavior now as far as I understand it.

2: I have tested the change here in #169 successfully in 3 contexts:

  • ESP32SPI*
  • Builtin wifi with ESP32-S2 TFT
  • CPython on a Linux PC.

The asterisks for ESP32SPI is because my testing was performed on the version of ESP32SPI from adafruit/Adafruit_CircuitPython_ESP32SPI#167 rather than the currently stock released version.

When using that modified ESP32SPI + The version from this PR the example scripts are working as intended and the blocking (or lack thereof) behavior of loop() appears to work correct to me.

On a somewhat related note, I do think it would be good to tweak the time value here:

to be a little bigger. During testing I found that after a few dozen or so successful publishes it stops working and seems to fail silently. The publish code is called but the feed on aio doesn't get the new data, and the message callback isn't called.

Im not certain of the root cause, but I started suspecting we were publishing data "too fast" and that was causing it to start failing. I backed that off to a 3 second sleep and it's been running steadily without having the same problem. With regards to question 1 above: this change is not necessarily required, but it does seem good for stability.

@brentru
Copy link
Member

brentru commented Jun 20, 2023

1: The examples do not need to change as a result of the change in this PR

EXCELLENT! Thank you for testing.

On a somewhat related note, I do think it would be good to tweak the time value here:

@vladak Are you able to increase the delay value on the line specified by @FoamyGuy? After that, I'd like to go ahead and merge this PR.

@vladak
Copy link
Contributor Author

vladak commented Jun 22, 2023

This is super awesome if someone comes to the rescue and tests the code in the PR for you ! Thanks @FoamyGuy ! I have to admit I fired off the PR primarily to get the discussion started and skimped on proper testing. That said, I will think about a unit test for this.

@vladak
Copy link
Contributor Author

vladak commented Jun 22, 2023

Im not certain of the root cause, but I started suspecting we were publishing data "too fast" and that was causing it to start failing. I backed that off to a 3 second sleep and it's been running steadily without having the same problem. With regards to question 1 above: this change is not necessarily required, but it does seem good for stability.

Okay, changed to 3 seconds.

@FoamyGuy
Copy link
Contributor

I'm doing some more testing on this and all the other changes associated with the ESP32SPI socket compatibility and I noticed that the mqtt_client.loop() function was never returning when used with Ethernet Featherwing if there was no data to recieve. It would never timeout successfully which made it effectively blocking until data does get recieved.

I opened: adafruit/Adafruit_CircuitPython_Wiznet5k#125 over there to address this and successfully tested the adafruitio ethernet example in this repo with those changes (and the changes from this PR).

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you @vladak!

Merging this one now along with the changes to ESP32SPI library and others that were all tied together in various ways.

@FoamyGuy FoamyGuy merged commit 6e3ed77 into adafruit:main Jul 8, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jul 9, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 6.0.0 from 5.0.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#167 from dhalbert/compatible-socket

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.4.0 from 7.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#169 from vladak/loop_vs_timeout
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#162 from adafruit/settings_dot_toml
  > Update .pylintrc, fix jQuery for docs
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#167 from tekktrik/main
  > Run pre-commit
  > Update pre-commit hooks
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#164 from zbauman3/zane/cert-key-pair-example

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 2.0.0 from 1.14.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#135 from FoamyGuy/remove_backward_compat

Updating https://github.com/adafruit/Adafruit_CircuitPython_WSGI to 2.0.0 from 1.1.16:
  > Merge pull request adafruit/Adafruit_CircuitPython_WSGI#20 from FoamyGuy/compatibility_refactor
  > Merge pull request adafruit/Adafruit_CircuitPython_WSGI#19 from tekktrik/dev/fix-packaging

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@vladak vladak mentioned this pull request Jul 24, 2023
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.

3 participants