8000 unix/modusocket: Raise ETIMEDOUT when connect or accept has timeout. · devmonkZA/micropython@8c9758f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c9758f

Browse files
committed
unix/modusocket: Raise ETIMEDOUT when connect or accept has timeout.
1 parent 887a671 commit 8c9758f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ports/unix/modusocket.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
160160
mp_buffer_info_t bufinfo;
161161
mp_get_buffer_raise(addr_in, &bufinfo, MP_BUFFER_READ);
162162
int r = connect(self->fd, (const struct sockaddr *)bufinfo.buf, bufinfo.len);
163-
RAISE_ERRNO(r, errno);
163+
int err = errno;
164+
if (r == -1 && self->blocking && err == EINPROGRESS) {
165+
// EINPROGRESS on a blocking socket means the operation timed out
166+
err = MP_ETIMEDOUT;
167+
}
168+
RAISE_ERRNO(r, err);
164169
return mp_const_none;
165170
}
166171
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
@@ -190,7 +195,12 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
190195
byte addr[32];
191196
socklen_t addr_len = sizeof(addr);
192197
int fd = accept(self->fd, (struct sockaddr*)&addr, &addr_len);
193-
RAISE_ERRNO(fd, errno);
198+
int err = errno;
199+
if (fd == -1 && self->blocking && err == EAGAIN) {
200+
// EAGAIN on a blocking socket means the operation timed out
201+
err = MP_ETIMEDOUT;
202+
}
203+
RAISE_ERRNO(fd, err);
194204

195205
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
196206
t->items[0] = MP_OBJ_FROM_PTR(socket_new(fd));

0 commit comments

Comments
 (0)
0