adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 36 forks source link

Out of sockets on 3rd Request #73

Closed jfurcean closed 3 years ago

jfurcean commented 3 years ago

The MagTag Google Calendar Event Display Learn Guide is crashing with the following trace using CircuitPython 6.2.0-beta.2 and the latest libraries.

"Traceback (most recent call last):
  File "code.py", line 252, in <module>
  File "code.py", line 129, in get_calendar_events
  File "adafruit_requests.py", line 598, in get
  File "adafruit_requests.py", line 559, in request
  File "adafruit_requests.py", line 436, in _get_socket
  File "adafruit_requests.py", line 433, in _get_socket
RuntimeError: Out of sockets"
jfurcean commented 3 years ago

This looks like a failure to close sockets. The below example crashes on the third request.

from adafruit_magtag.magtag import MagTag

magtag = MagTag()
magtag.network.connect()

requests = magtag.network.requests

urls = ["https://wired.com/", "https://www.test.com/","https://apple.com/"]

for url in urls:
    print(url)
    resp = requests.get(url)

    resp.close()
Traceback (most recent call last):
  File "code.py", line 12, in <module>
  File "adafruit_requests.py", line 616, in get
  File "adafruit_requests.py", line 575, in request
  File "adafruit_requests.py", line 445, in _get_socket
  File "adafruit_requests.py", line 441, in _get_socket
RuntimeError: Out of sockets
anecdata commented 3 years ago

Just for completeness, it's unrelated to MagTag. This also gets Out of sockets:

import wifi
import socketpool
import ssl
import adafruit_requests
from secrets import secrets

wifi.radio.connect(secrets['ssid'], secrets['password'])
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

urls = ["https://wired.com/", "https://www.test.com/", "https://apple.com/"]

for url in urls:
    print(url)
    resp = requests.get(url)
    resp.close()
jfurcean commented 3 years ago

This should be fixed by PR #74.