Skip to content

Commit cc6dbb3

Browse files
authored
Merge pull request #7455 from dhalbert/ensure-nonblocking-socket
espressif: always make new client sockets be non-blocking
2 parents 691d6d8 + 40534da commit cc6dbb3

File tree

1 file changed

+10
-4
lines changed
  • ports/espressif/common-hal/socketpool

1 file changed

+10
-4
lines changed

ports/espressif/common-hal/socketpool/Socket.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ STATIC void unregister_open_socket(int fd) {
168168
if (open_socket_fds[i] == fd) {
169169
open_socket_fds[i] = -1;
170170
user_socket[i] = false;
171-
write(socket_change_fd, &fd, sizeof(fd));
171+
// Write must be 8 bytes for an eventfd.
172+
uint64_t signal = 1;
173+
write(socket_change_fd, &signal, sizeof(signal));
172174
return;
173175
}
174176
}
@@ -251,7 +253,9 @@ int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_
251253
uint64_t start_ticks = supervisor_ticks_ms64();
252254

253255
// Allow timeouts and interrupts
254-
while (newsoc == -1 && !timed_out) {
256+
while (newsoc == -1 &&
257+
!timed_out &&
258+
!mp_hal_is_interrupted()) {
255259
if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) {
256260
timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms;
257261
}
@@ -263,6 +267,9 @@ int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_
263267
}
264268
}
265269

270+
// New client socket will not be non-blocking by default, so make it non-blocking.
271+
lwip_fcntl(newsoc, F_SETFL, O_NONBLOCK);
272+
266273
if (!timed_out) {
267274
// harmless on failure but avoiding memcpy is faster
268275
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_
284291
if (!common_hal_socketpool_socket_get_closed(accepted)) {
285292
common_hal_socketpool_socket_close(accepted);
286293
}
287-
// Create the socket
294+
// Replace the old accepted socket with the new one.
288295
accepted->num = newsoc;
289296
accepted->pool = self->pool;
290297
accepted->connected = true;
291-
lwip_fcntl(newsoc, F_SETFL, O_NONBLOCK);
292298
}
293299

294300
return newsoc;

0 commit comments

Comments
 (0)