-
Notifications
You must be signed in to change notification settings - Fork 35
Getting "Repeated socket failures" on PicoW #126
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
Comments
From some further debugging, it seems that it works before first power cycle is performed. I also tried MQTT and from the logs it seems like the Pico is capable of transmitting the data to the broker, it is not able to receive data and fails with the following message.
I am starting to think, that this problem is connected to something in the wifi driver and I should probably move this issue to the CircuitPython repo. |
@matoushybl can you try this code, it bypasses requests (we can debug requests in detail depending how this comes out): import os
import wifi
import socketpool
import ssl
import ipaddress
from secrets import secrets
HOST = "wifitest.adafruit.com"
PATH = "/testwifi/index.html"
PORT = 80
TIMEOUT = 60
MAXBUF = 1024
print("Connecting to wifi")
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
print("Create TCP Client Socket")
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
s.settimeout(TIMEOUT)
print("Connecting")
s.connect((HOST, PORT))
size = s.send(f"GET {PATH} HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode())
print("Sent", size, "bytes")
# just get the first hunk and call it a day
buf = bytearray(MAXBUF)
size = s.recv_into(buf)
print('Received', size, "bytes", buf[:size])
s.close()
print("Done-ish.") |
I'll do that. I made some progress yesterday when I came across a similar issue here: adafruit/circuitpython#7333 . It seems like when I disable the web workflow by not specifying the SSID and password with the |
First running with
After powerclycling:
Works flawlessy after removing the SSID and password prefix and power cycling. |
It sounds like you are not connected to wifi. The You can use those names, or anything you like, for basic connection outside of web workflow. But in any case, the You can verify that you are connected by printing or checking |
I am pretty sure, I am connected to the wifi. I am sorry for misleading communication. import os
import wifi
import socketpool
import ssl
import ipaddress
HOST = "wifitest.adafruit.com"
PATH = "/testwifi/index.html"
PORT = 80
TIMEOUT = 60
MAXBUF = 1024
print("Connecting to wifi")
wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
print("Create TCP Client Socket")
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
s.settimeout(TIMEOUT)
print("Connecting")
s.connect((HOST, PORT))
size = s.send(f"GET {PATH} HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode())
print("Sent", size, "bytes")
# just get the first hunk and call it a day
buf = bytearray(MAXBUF)
size = s.recv_into(buf)
print('Received', size, "bytes", buf[:size])
s.close()
print("Done-ish.") settings.toml:
output:
Here is what doesn't work for me: import os
import wifi
import socketpool
import ssl
import ipaddress
HOST = "wifitest.adafruit.com"
PATH = "/testwifi/index.html"
PORT = 80
TIMEOUT = 60
MAXBUF = 1024
print("Connecting to wifi")
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print("My IP address is", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
print("Create TCP Client Socket")
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
s.settimeout(TIMEOUT)
print("Connecting")
s.connect((HOST, PORT))
size = s.send(f"GET {PATH} HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode())
print("Sent", size, "bytes")
# just get the first hunk and call it a day
buf = bytearray(MAXBUF)
size = s.recv_into(buf)
print('Received', size, "bytes", buf[:size])
s.close()
print("Done-ish.") settings.toml:
First output is as follows - everything works:
After power cycling the board:
This behavior is consistent over multiple reboots and runs of the code. |
When web workflow is on and it works the first time... what are the conditions preceding that (what had it been doing until that time? random?) vs. after a fresh power cycle? Is it the same if the board is reset using I haven't seen this behavior since a later beta-5, but I don't know what core change improved it at that time. But presumably some condition(s) in your environment might be bringing it out. |
BTW, I added some debugging to requests around line 650 or so in the current version: # We may fail to send the request if the socket we got is closed already. So, try a second
# time in that case.
retry_count = 0
last_exc = None
while retry_count < 2:
retry_count += 1
socket = self._get_socket(host, port, proto, timeout=timeout)
print(f"#### DEBUG {time.monotonic():.3f} _get_socket {host} {port} {proto} {timeout}")
ok = True
try:
self._send_request(socket, host, method, path, headers, data, json)
print(f"#### DEBUG {time.monotonic():.3f} _send_request SENT {socket} {host} {method} {path} {headers}") # {data} {json}
except OSError as exc:
print(f"#### DEBUG {time.monotonic():.3f} _send_request")
traceback.print_exception(exc, exc, exc.__traceback__)
ok = False
if ok:
# Read the H of "HTTP/1.1" to make sure the socket is alive. send can appear to work
# even when the socket is closed.
if hasattr(socket, "recv"):
result = socket.recv(1)
else:
result = bytearray(1)
try:
socket.recv_into(result)
except OSError as exc:
print(f"#### DEBUG {time.monotonic():.3f} recv_into")
traceback.print_exception(exc, exc, exc.__traceback__)
pass
if result == b"H":
# Things seem to be ok so break with socket set.
break
else:
print(f"#### DEBUG {time.monotonic():.3f} result NOT == {b'H'}")
else:
print(f"#### DEBUG {time.monotonic():.3f} NOT OK")
self._close_socket(socket)
socket = None
if not socket:
raise OutOfRetries("Repeated socket failures") from last_exc that gives a little more detail on the underlying errors (need to add imports for time and traceback) |
@matoushybl Can you try adding a delay like |
I tried adding the delay, but it didn't change anything. I have also updated to CircuitPython 8.0.0. |
with the 5-second delay in, it doesn't work even after a power cycle or (for example, web workflow enabled with settings.toml, this example code: |
That code works, interesting. I wasn't able to make it fail during a short testing. |
Good news. The key is that after a reset (reset button, power cycle, or Hopefully, this can be addressed in the core so code doesn't need these delays. There is an open issue for that (7333). But closing this library issue for now since we believe it's the same as the core issue, and the workaround |
@anecdata I'm confused. Are you saying that the delay solves the problem? But I would consider that a workaround, since we'd have to document that. We need some test that says the CYW43 is really ready, and then we can do that invisibly. I wonder if there is some transaction going on with the access point that needs to finish, but the CYW43 is not revealing that it's not in a good state before that. |
I just thought this library issue was redundant with the core issue. Any real solution isn't in the library. We can re-open if that's more appropriate. I don't know what aspect of the CYW43 startup is going bad (and staying bad) when not enough time is allowed. |
ah sorry, I missed that this was the library issue |
@matoushybl This has been fixed with adafruit/circuitpython#7589 and should be in the S3 Absolute Newest soon. |
Thanks! I am looking forward to it. |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I am trying to use this library on a Pico W with CircuitPython 8.0.0 beta 6 and I also tried nightly. The problem is that most of the time, I am getting "OutOfRetries: Repeated socket failures" when trying to GET basic URLs. The code is as follows:
The output is:
Randomly, the request is ok, but after rerunning the code it fails. Power cycling the board doesn't work and I've tried multiple APs.
Can you please give me any pointers on how to debug this problem?
Thanks!
The text was updated successfully, but these errors were encountered: