circuitpython: Sending Requests Failed on second network call in CircuitPython 7.0.0

This was originally found by a user in the forums at https://forums.adafruit.com/viewtopic.php?f=19&t=184484&p=895677. I was able to reproduce the issue by following the guide at https://learn.adafruit.com/magtag-google-calendar-event-display.

Here’s the stack trace

Traceback (most recent call last):
File "code.py", line 246, in <module>
File "code.py", line 84, in get_current_time
File "adafruit_portalbase/__init__.py", line 411, in get_local_time
File "adafruit_portalbase/network.py", line 231, in get_local_time
File "adafruit_portalbase/network.py", line 200, in get_strftime
File "adafruit_requests.py", line 613, in get
File "adafruit_requests.py", line 554, in request
File "adafruit_requests.py", line 423, in _get_socket
RuntimeError: Sending request failed

After making is so get_local_time was only called once, it failed on the next network call with this stack trace:

Traceback (most recent call last):
  File "code.py", line 227, in <module>
  File "adafruit_oauth2.py", line 179, in refresh_access_token
  File "adafruit_requests.py", line 617, in post
  File "adafruit_requests.py", line 554, in request
  File "adafruit_requests.py", line 423, in _get_socket
RuntimeError: Sending request failed

The first call is successful, but any subsequent calls fail. This only happens in CircuitPython 7.0.0 and not 6.3.0. I have not tried any of the alpha, beta, or release candidate versions in between.

Note: This issue may need to be moved to the CircuitPython repo

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (2 by maintainers)

Most upvoted comments

Thanks @tbjers ! That helped a lot. For those who might want to see a full working example that runs fine on CircuitPython v7.1.0: https://gist.github.com/JannieT/1e589b215ec05118ab78ec5d441c69e7

@makermelissa This issue is just an issue with the example. I have traced through the code and discovered:

  1. There can only be one socket_pool per wifi.radio
  2. Initializing the radio twice isn’t helping
  3. The fix can be done entirely in code.py using these libraries

My only minor gripe is that I have to load secrets inside my code. It would be great if there was a public accessor or a way to pass the secrets through MagTag into PortalBase and NetworkBase. That way they can be read once, and be one source of truth.

Here’s how to fix this issue (verified on a MagTag running CircuitPython 7.1.0-beta.1):

Remove initialization of wifi and requests in code.py.

Move MagTag init above OAuth2 init.

Call:

magtag.network.connect()

# Initialize an OAuth2 object with GCal API scope
scopes = ["https://www.googleapis.com/auth/calendar.readonly"]
google_auth = OAuth2(
    magtag.network.requests,
    secrets["google_client_id"],
    secrets["google_client_secret"],
    scopes,
    secrets["google_access_token"],
    secrets["google_refresh_token"],
)

Then update:

def get_calendar_events #...

to use:

magtag.network.requests.get()

and everything works swimmingly!

@anecdata it still fails from then on.

I’ve narrowed it down to the failure first occurring between CircuitPython 7 Alpha 3 and 4.

My brain has purged all requests context. Sorry!