Skip to content

Commit 7fad382

Browse files
nickzoicdpgeorge
authored andcommitted
esp32/modsocket.c: add comment explaining timeout behaviour
1 parent 9a89cba commit 7fad382

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

esp32/modsocket.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
212212
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
213213

214214
void _socket_settimeout(socket_obj_t *sock, uint64_t timeout_ms) {
215-
sock->retries = timeout_ms * 1000 / SOCKET_POLL_US;
215+
// Rather than waiting for the entire timeout specified, we wait sock->retries times
216+
// for SOCKET_POLL_US each, checking for a MicroPython interrupt between timeouts.
217+
// with SOCKET_POLL_MS == 100ms, sock->retries allows for timeouts up to 13 years.
218+
// if timeout_ms == UINT64_MAX, wait forever.
219+
sock->retries = (timeout_ms == UINT64_MAX) ? UINT_MAX : timeout_ms * 1000 / SOCKET_POLL_US;
216220

217221
struct timeval timeout = {
218222
.tv_sec = 0,
@@ -225,15 +229,15 @@ void _socket_settimeout(socket_obj_t *sock, uint64_t timeout_ms) {
225229

226230
STATIC mp_obj_t socket_settimeout(const mp_obj_t arg0, const mp_obj_t arg1) {
227231
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
228-
if (arg1 == mp_const_none) _socket_settimeout(self, UINT_MAX);
232+
if (arg1 == mp_const_none) _socket_settimeout(self, UINT64_MAX);
229233
else _socket_settimeout(self, mp_obj_get_float(arg1) * 1000L);
230234
return mp_const_none;
231235
}
232236
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_settimeout_obj, socket_settimeout);
233237

234238
STATIC mp_obj_t socket_setblocking(const mp_obj_t arg0, const mp_obj_t arg1) {
235239
socket_obj_t *self = MP_OBJ_TO_PTR(arg0);
236-
if (mp_obj_is_true(arg1)) _socket_settimeout(self, UINT_MAX);
240+
if (mp_obj_is_true(arg1)) _socket_settimeout(self, UINT64_MAX);
237241
else _socket_settimeout(self, 0);
238242
return mp_const_none;
239243
}
@@ -462,7 +466,7 @@ STATIC mp_obj_t get_socket(size_t n_args, const mp_obj_t *args) {
462466
if (sock->fd < 0) {
463467
exception_from_errno(errno);
464468
}
465-
_socket_settimeout(sock, UINT_MAX);
469+
_socket_settimeout(sock, UINT64_MAX);
466470

467471
return MP_OBJ_FROM_PTR(sock);
468472
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy