Skip to content

Commit

Permalink
esp32/modsocket: Try garbage collection if the socket limit is reached.
Browse files Browse the repository at this point in the history
If the hard socket limit (default 16) is reached then it's possible that
socket allocation fails but garbage collection would allow it to succeed.

Perform a GC pass and try again before giving up, similar to the logic
elsewhere in MicroPython that tries a GC pass before raising MemoryError.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
  • Loading branch information
projectgus authored and dpgeorge committed Nov 20, 2023
1 parent 57cce79 commit fce8d9f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ports/esp32/modsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>

#include "py/gc.h"
#include "py/runtime0.h"
#include "py/nlr.h"
#include "py/objlist.h"
Expand Down Expand Up @@ -274,6 +275,13 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, siz
sock->state = sock->type == SOCK_STREAM ? SOCKET_STATE_NEW : SOCKET_STATE_CONNECTED;

sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
if (sock->fd < 0 && errno == ENFILE) {
// ESP32 LWIP has a hard socket limit, ENFILE is returned when this is
// reached. Similar to the logic elsewhere for MemoryError, try running
// GC before failing outright.
gc_collect();
sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
}
if (sock->fd < 0) {
mp_raise_OSError(errno);
}
Expand Down

0 comments on commit fce8d9f

Please sign in to comment.