From a4eef90b2274d2e34486b6990191687500cb3893 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 7 Jun 2022 17:36:28 +0200 Subject: [PATCH] extmod/modusocket: Fix polling of closed sockets. Unbound sockets in NEW state should return HUP|WR when polled, and return NVAL when in CLOSED state. --- extmod/modusocket.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/extmod/modusocket.c b/extmod/modusocket.c index f15c98fc05..638ab0cf28 100644 --- a/extmod/modusocket.c +++ b/extmod/modusocket.c @@ -501,14 +501,19 @@ mp_uint_t socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int * if (self->nic != MP_OBJ_NULL) { self->nic_type->close(self); self->nic = MP_OBJ_NULL; - self->state = MOD_NETWORK_SS_CLOSED; } + self->state = MOD_NETWORK_SS_CLOSED; return 0; } if (self->nic == MP_OBJ_NULL) { if (request == MP_STREAM_POLL) { - // New sockets are writable and not connected. - return MP_STREAM_POLL_HUP | MP_STREAM_POLL_WR; + if (self->state == MOD_NETWORK_SS_NEW) { + // New sockets are writable and not connected. + return MP_STREAM_POLL_HUP | MP_STREAM_POLL_WR; + } else if (self->state == MOD_NETWORK_SS_CLOSED) { + // Closed socket, return invalid. + return MP_STREAM_POLL_NVAL; + } } *errcode = MP_EINVAL; return MP_STREAM_ERROR;