circuitpython: Calling wifi.radio.connect more than once causes wifi to disconnect

Attached is a very slimmed down example where you cannot reuse IO_HTTP. This might be a WIFI or a Request issue so I’m opening here.

Originally, I was sending data from a sensor, but you can reproduce this just using the time server on AIO. sleep_bug.txt

The gist of the issues is:

  1. You can not call AIO more than 2 times
  2. You get different behavior if you setup your pool, request, AIO inside or outside the loop. Not sure which is supposed to be reliable.

I can reproduce this on the MetroS2 and on the Kaluga. Both running a build from 2020-11-20 of Main.

This is the setup block

    ## SETUP BLOCK ##
    print("Setup Requests & IO_HTTP")
    POOL = socketpool.SocketPool(wifi.radio)
    REQUESTS = adafruit_requests.Session(POOL, ssl.create_default_context())
    # Initialize an Adafruit IO HTTP API object
    AIO = IO_HTTP(config.ADAFRUIT_IO_USERNAME, config.ADAFRUIT_IO_KEY, REQUESTS)
    ## SETUP BLOCK ##

POOL, REQUESTS AND AIO outside the While works 1 time.

REPL

code.py output:
Get on the network
ip: 192.168.1.246
hostname: Metro-ESP32S2
Setup Requests & IO_HTTP

Ping: 0.016
AIO Time: struct_time(tm_year=2020, tm_mon=11, tm_mday=20, tm_hour=19, tm_min=25, tm_sec=21, tm_wday=5, tm_yday=325, tm_isdst=0)
Sleeping for 30 seconds.

Ping: 0.013
Traceback (most recent call last):
  File "code.py", line 31, in <module>
  File "/lib/adafruit_io/adafruit_io.py", line 701, in receive_time
  File "/lib/adafruit_io/adafruit_io.py", line 542, in _get
  File "adafruit_requests.py", line 594, in get
  File "adafruit_requests.py", line 581, in request
  File "adafruit_requests.py", line 119, in __init__
RuntimeError: Unable to read HTTP response.

Debug Output from Kaluga

W (9669) wifi: got ip
I (10639) esp-x509-crt-bundle: Certificate validated
I (102699) wifi:state: run -> init (0)
I (102699) wifi:pm stop, total sleep time: 92188662 us / 95776179 us

I (102699) wifi:new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1
W (102699) wifi:hmac tx: stop, discard
W (102709) wifi: disconnected
W (102709) wifi: reason 8 0x08
I (102739) wifi:flush txq
I (102739) wifi:stop sw txq
I (102739) wifi:lmac stop hw txq
I (102739) wifi:Deinit lldesc rx mblock:4
I (102749) gpio: GPIO[45]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0

POOL, REQUESTS AND AIO inside the While works 2 times.

Note: wifi: reason 8 0x08 WIFI_REASON_ASSOC_LEAVE

REPL

code.py output:
Get on the network
ip: 192.168.1.246
hostname: Metro-ESP32S2

Ping: 0.011
Setup Requests
AIO Time: struct_time(tm_year=2020, tm_mon=11, tm_mday=20, tm_hour=19, tm_min=22, tm_sec=46, tm_wday=5, tm_yday=325, tm_isdst=0)
Sleeping for 30 seconds.

Ping: 0.013
Setup Requests
AIO Time: struct_time(tm_year=2020, tm_mon=11, tm_mday=20, tm_hour=19, tm_min=23, tm_sec=19, tm_wday=5, tm_yday=325, tm_isdst=0)
Sleeping for 30 seconds.

Ping: None
Setup Requests
Traceback (most recent call last):
  File "code.py", line 28, in <module>
  File "/lib/adafruit_io/adafruit_io.py", line 701, in receive_time
  File "/lib/adafruit_io/adafruit_io.py", line 542, in _get
  File "adafruit_requests.py", line 594, in get
  File "adafruit_requests.py", line 572, in request
  File "adafruit_requests.py", line 441, in _get_socket
RuntimeError: Sending request failed

Debug output from the Kaluga

W (248669) wifi: got ip
I (249639) esp-x509-crt-bundle: Certificate validated
I (282819) esp-x509-crt-bundle: Certificate validated
E (318099) esp-tls-mbedtls: mbedtls_ssl_setup returned -0x7f00
E (318099) esp-tls: create_ssl_handle failed
E (318099) esp-tls: Failed to open new connection
I (318179) wifi:state: run -> init (0)
I (318179) wifi:pm stop, total sleep time: 67591551 us / 72243520 us

I (318179) wifi:new:<6,0>, old:<6,0>, ap:<255,255>, sta:<6,0>, prof:1
W (318179) wifi:hmac tx: stop, discard
W (318189) wifi:hmac tx: stop, discard
W (318189) wifi: disconnected
W (318189) wifi: reason 8 0x08
I (318229) wifi:flush txq
I (318229) wifi:stop sw txq
I (318229) wifi:lmac stop hw txq
I (318229) wifi:Deinit lldesc rx mblock:4
I (318239) gpio: GPIO[45]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0

Note: wifi: reason 8 0x08 WIFI_REASON_ASSOC_LEAVE

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

I’m also fine with being told “don’t do that”.

We should have CircuitPython tell you not to do that. We should track if the radio already has a pool and raise an exception on the second init. We’ll need to add a deinit to SocketPool too just in case you want to deinit and recreate. This matches how most of our APIs hold exclusive access to things.

I’m also fine with being told “don’t do that”. I do think as we get into power saving sleep it might be needed to call it again and raise the same issues, but I’ve not tried it yet. That is next on my todo list though. I’ll open a new issue for it and offer my approval based on the testing on the PR.