adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.11k stars 1.22k forks source link

Memory Error when sending a get_request AFTER using http server with AP on CP9 beta 2 #8968

Open louisprp opened 8 months ago

louisprp commented 8 months ago

CircuitPython version

Adafruit CircuitPython 9.0.0-beta.2 on 2024-02-20; Waveshare ESP32-S2-Pico with ESP32S2

Code/REPL

### Starting the Server

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = MDNS_HOSTNAME
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)

pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool, "/static", debug=True)

@server.route("/")
def base(request: Request):
    return Response(request, "Hello from the CircuitPython HTTP Server!")

server.start(str(wifi.radio.ipv4_address_ap), 80)

### Stopping the Server

server.stop()
mdns_server.deinit()
wifi.radio.stop_ap()

### Then using requests (connected to Wi-Fi with internet connection)

try:
    url = "https://www.adafruit.com/api/quotes.php"
    #  pings adafruit quotes
    print("Fetching text from %s" % url)
    #  gets the quote from adafruit quotes
    response = requests.get(url)
    print("-" * 40)
    #  prints the response to the REPL
    print("Headers: ", response.headers)
    print("-" * 40)
    response.close()
except Exception as e:
    print("Error:\n", str(e))

Behavior

Traceback (most recent call last):
  File "requests.py", line 139, in get_info
  File "adafruit_requests.py", line 752, in get
  File "adafruit_requests.py", line 691, in request
  File "adafruit_requests.py", line 510, in _get_socket
MemoryError: 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "code.py", line 132, in <module>
  File "requests.py", line 148, in get_info

Description

It seems that there is a MemoryError when doing a get request after using the socket (?) for an http server. I'm utilizing the server along with the AP. When Im not starting the server beforehand, the requests work fine. Not sure what the problem is here. Under previous version 8.x this seems to have worked fine on the ESP32-S2.

Additional information

No response

tannewt commented 8 months ago

Please post the complete example. This one has no imports. I wasn't able to reproduce it because they are missing.

dhalbert commented 6 months ago

Could you retry with the latest ConnectionManager library? Thanks. https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager/pull/16 has fixed several other issues that seem similar.