From 40534da92a8e385b9a04b8d0ff14f8365b2e1f5a Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 14 Jan 2023 16:36:33 -0500 Subject: [PATCH] espressif: always make new client sockets be non-blocking --- ports/espressif/common-hal/socketpool/Socket.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ports/espressif/common-hal/socketpool/Socket.c b/ports/espressif/common-hal/socketpool/Socket.c index ccb01116d8..8b6cff30ef 100644 --- a/ports/espressif/common-hal/socketpool/Socket.c +++ b/ports/espressif/common-hal/socketpool/Socket.c @@ -168,7 +168,9 @@ STATIC void unregister_open_socket(int fd) { if (open_socket_fds[i] == fd) { open_socket_fds[i] = -1; user_socket[i] = false; - write(socket_change_fd, &fd, sizeof(fd)); + // Write must be 8 bytes for an eventfd. + uint64_t signal = 1; + write(socket_change_fd, &signal, sizeof(signal)); return; } } @@ -251,7 +253,9 @@ int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_ uint64_t start_ticks = supervisor_ticks_ms64(); // Allow timeouts and interrupts - while (newsoc == -1 && !timed_out) { + while (newsoc == -1 && + !timed_out && + !mp_hal_is_interrupted()) { if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) { timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms; } @@ -263,6 +267,9 @@ int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_ } } + // New client socket will not be non-blocking by default, so make it non-blocking. + lwip_fcntl(newsoc, F_SETFL, O_NONBLOCK); + if (!timed_out) { // harmless on failure but avoiding memcpy is faster memcpy((void *)ip, (void *)&accept_addr.sin_addr.s_addr, sizeof(accept_addr.sin_addr.s_addr)); @@ -284,11 +291,10 @@ int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_ if (!common_hal_socketpool_socket_get_closed(accepted)) { common_hal_socketpool_socket_close(accepted); } - // Create the socket + // Replace the old accepted socket with the new one. accepted->num = newsoc; accepted->pool = self->pool; accepted->connected = true; - lwip_fcntl(newsoc, F_SETFL, O_NONBLOCK); } return newsoc;