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

Re-try in more cases when socket cannot first be created #16

Merged
merged 3 commits into from May 12, 2024

Conversation

dhalbert
Copy link
Contributor

Recover in more cases when a socket cannot be created. This problem is particularly acute on ESP32-S2 on CircuitPython 9.x.x. Use a 9.1.0 build past beta.1 if you would like user-specifiable timeouts. Testing welcome by anyone.

This appears to fix adafruit/circuitpython#9219.

Also:

  • Clarify some documentation. Use sphinx argument documentation style.
  • Fix some typos.
  • Remove a few internal comments marking code sections.
  • Clarify an error message.
  • Internally, catch exceptions instead of passing them back.
  • Change one exception.
  • Update to pylint 3.1.0 so pre-commit can run under Python 3.12.

Test program:

import gc
import os
import wifi
import socketpool
import ssl


import adafruit_connection_manager
import adafruit_requests

# Get WiFi details, ensure these are setup in settings.toml
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

# Without ConnectionManager
# pool = socketpool.SocketPool(wifi.radio)
# ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)

pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = ssl.create_default_context()

requests = adafruit_requests.Session(pool, ssl_context)

wifi.radio.connect(ssid, password)

for url in (
    # bunch of failures: failures vs working doesn't really matter
    "https://192.168.1.253",
    "https://192.168.1.252",
    "https://192.168.1.251",
    "https://192.168.1.250",
    "https://192.168.1.249",
    "https://192.168.1.248",
    "https://192.168.1.247",
    "https://192.168.1.246",
    "https://192.168.1.245",
    # various working sites
    "https://example.org",
    "https://example.net",
    "https://example.com",
):
    print(url)
    try:
        response = requests.get(url, timeout=2)
        #print(response.text)
    except Exception as e:
        print(type(e), e)

Also:
- Clarify some documentation. Use sphinx argument documentation style.
- Fix some typos.
- Remove a few internal comments marking code sections.
- Clarify an error message.
- Internally, catch exceptions instead of passing them back.
- Change one exception.
- Update to pylint 3.1.0 so pre-commit can run under Python 3.12
@dhalbert dhalbert requested review from justmobilize and a team May 10, 2024 21:00
adafruit_connection_manager.py Show resolved Hide resolved
adafruit_connection_manager.py Show resolved Hide resolved
adafruit_connection_manager.py Outdated Show resolved Hide resolved
adafruit_connection_manager.py Show resolved Hide resolved
adafruit_connection_manager.py Show resolved Hide resolved
@dhalbert dhalbert force-pushed the socket-retry branch 2 times, most recently from 49bc2ec to d58ab6b Compare May 11, 2024 01:53
@justmobilize
Copy link
Collaborator

@dhalbert tested and looks good. Quick comments:

  1. Added a question on tests
  2. I assume this will be a major release as the exceptions returned are different
  3. Created PRs for requests and minimqtt as these changes will break tests

@dhalbert
Copy link
Contributor Author

dhalbert commented May 12, 2024

I assume this will be a major release as the exceptions returned are different

Yes, I will do that, though since it was error returns on bad args, I don't think it will affect much code.

@dhalbert dhalbert merged commit 0a4f745 into adafruit:main May 12, 2024
1 check passed
@dhalbert dhalbert deleted the socket-retry branch May 12, 2024 14:33
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 13, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager to 3.0.0 from 2.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ConnectionManager#16 from dhalbert/socket-retry

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.7.0 from 7.6.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#212 from justmobilize/exception-test-updates

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 3.2.9 from 3.2.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#192 from justmobilize/exception-test-updates

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
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.

ESP32-S2: only one socket can be created
2 participants