diff options
| author | Damien George <damien@micropython.org> | 2024-06-07 13:33:47 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2024-06-08 09:02:01 +1000 |
| commit | df0d7e942986b65ed42fbbf75712639f4a83dc0e (patch) | |
| tree | 8bd79d50b91a4df4dcebd31a20587dea8686d5ee /extmod/modlwip.c | |
| parent | 80a4f632ee812de9f2b22e88abb36ca36d230ec0 (diff) | |
extmod/modlwip: Make socket.connect raise ETIMEDOUT on non-zero timeout.
If the socket timeout is 0 then a failed socket.connect() raises
EINPROGRESS (which is what the lwIP bindings already did), but if the
socket timeout is non-zero then a failed socket.connect() should raise
ETIMEDOUT. The latter is fixed in this commit.
A test is added for these timeout cases.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'extmod/modlwip.c')
| -rw-r--r-- | extmod/modlwip.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/extmod/modlwip.c b/extmod/modlwip.c index afac512e8..1ae8c114c 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -326,6 +326,10 @@ typedef struct _lwip_socket_obj_t { int8_t state; } lwip_socket_obj_t; +static inline bool socket_is_timedout(lwip_socket_obj_t *socket, mp_uint_t ticks_start) { + return socket->timeout != -1 && (mp_uint_t)(mp_hal_ticks_ms() - ticks_start) >= socket->timeout; +} + static inline void poll_sockets(void) { mp_event_wait_ms(1); } @@ -1130,21 +1134,21 @@ static mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { MICROPY_PY_LWIP_EXIT // And now we wait... - if (socket->timeout != -1) { - for (mp_uint_t retries = socket->timeout / 100; retries--;) { - mp_hal_delay_ms(100); - if (socket->state != STATE_CONNECTING) { - break; - } - } - if (socket->state == STATE_CONNECTING) { - mp_raise_OSError(MP_EINPROGRESS); + mp_uint_t ticks_start = mp_hal_ticks_ms(); + for (;;) { + poll_sockets(); + if (socket->state != STATE_CONNECTING) { + break; } - } else { - while (socket->state == STATE_CONNECTING) { - poll_sockets(); + if (socket_is_timedout(socket, ticks_start)) { + if (socket->timeout == 0) { + mp_raise_OSError(MP_EINPROGRESS); + } else { + mp_raise_OSError(MP_ETIMEDOUT); + } } } + if (socket->state == STATE_CONNECTED) { err = ERR_OK; } else { |
