@@ -212,7 +212,11 @@ STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
212
212
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (socket_setsockopt_obj , 4 , 4 , socket_setsockopt );
213
213
214
214
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 ;
216
220
217
221
struct timeval timeout = {
218
222
.tv_sec = 0 ,
@@ -225,15 +229,15 @@ void _socket_settimeout(socket_obj_t *sock, uint64_t timeout_ms) {
225
229
226
230
STATIC mp_obj_t socket_settimeout (const mp_obj_t arg0 , const mp_obj_t arg1 ) {
227
231
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 );
229
233
else _socket_settimeout (self , mp_obj_get_float (arg1 ) * 1000L );
230
234
return mp_const_none ;
231
235
}
232
236
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (socket_settimeout_obj , socket_settimeout );
233
237
234
238
STATIC mp_obj_t socket_setblocking (const mp_obj_t arg0 , const mp_obj_t arg1 ) {
235
239
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 );
237
241
else _socket_settimeout (self , 0 );
238
242
return mp_const_none ;
239
243
}
@@ -462,7 +466,7 @@ STATIC mp_obj_t get_socket(size_t n_args, const mp_obj_t *args) {
462
466
if (sock -> fd < 0 ) {
463
467
exception_from_errno (errno );
464
468
}
465
- _socket_settimeout (sock , UINT_MAX );
469
+ _socket_settimeout (sock , UINT64_MAX );
466
470
467
471
return MP_OBJ_FROM_PTR (sock );
468
472
}
0 commit comments