Closed
Description
In standard Python, socket.settimeout() sets a timeout for both TCP connections and data transfer. In CircuitPython, socket.settimeout() sets a timeout for data transfer, but doesn't affect the TCP connection timeout. It sure would be nice to set a TCP connection timeout, either via settimeout() or some other method.
For example, using CircuitPython 8.2.2 on an Adafruit Feather ESP32-S3 (no PSRAM), an attempted TCP connection always times out after 30 seconds, regardless of the settimeout() value. See below.
# Connection timeout demo
import board
import adafruit_sht31d
import wifi
import time
from socketpool import SocketPool
SSID = "[Wi-Fi name]"
PWD = "[Wi-Fi password]"
SERVER = "10.0.0.119" # any non-existent local address
PORT = 4790 # any arbitrary port
wifi.radio.connect(SSID, PWD, timeout=10)
starttime = time.monotonic()
try:
pool = SocketPool(wifi.radio)
with pool.socket(SocketPool.AF_INET, SocketPool.SOCK_STREAM) as s:
s.settimeout(5) # Does not affect connection timeout
s.connect((SERVER, PORT))
s.sendall(b"Data goes here")
except OSError as ex:
elapsed = time.monotonic() - starttime
print(f"Connection timeout in {elapsed} seconds.")
The example code output is "Connection timeout in 30.5 seconds." (give or take a couple of tenths), regardless of the settimeout() value.