Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fce8d9f

Browse files
projectgusdpgeorge
authored andcommitted
esp32/modsocket: Try garbage collection if the socket limit is reached.
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 <[email protected]>
1 parent 57cce79 commit fce8d9f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

ports/esp32/modsocket.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <stdlib.h>
3737
#include <string.h>
3838

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

276277
sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
278+
if (sock->fd < 0 && errno == ENFILE) {
279+
// ESP32 LWIP has a hard socket limit, ENFILE is returned when this is
280+
// reached. Similar to the logic elsewhere for MemoryError, try running
281+
// GC before failing outright.
282+
gc_collect();
283+
sock->fd = lwip_socket(sock->domain, sock->type, sock->proto);
284+
}
277285
if (sock->fd < 0) {
278286
mp_raise_OSError(errno);
279287
}

0 commit comments

Comments
 (0)