Skip to content

Commit 9b3186b

Browse files
authored
Merge pull request #3776 from tannewt/backup_socket_timeout
Two minor socket changes
2 parents 31acfed + 9276244 commit 9b3186b

File tree

1 file changed

+12
-3
lines changed
  • ports/esp32s2/common-hal/socketpool

1 file changed

+12
-3
lines changed

ports/esp32s2/common-hal/socketpool/Socket.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const c
6464
} else {
6565
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result);
6666
}
67+
} else {
68+
// Connection successful, set the timeout on the underlying socket. We can't rely on the IDF
69+
// to do it because the config structure is only used for TLS connections. Generally, we
70+
// shouldn't hit this timeout because we try to only read available data. However, there is
71+
// always a chance that we try to read something that is used internally.
72+
int fd;
73+
esp_tls_get_conn_sockfd(self->tcp, &fd);
74+
struct timeval tv;
75+
tv.tv_sec = 2 * 60; // Two minutes
76+
tv.tv_usec = 0;
77+
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
78+
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
6779
}
6880

6981
return self->connected;
@@ -123,9 +135,6 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
123135
// socket closed
124136
common_hal_socketpool_socket_close(self);
125137
}
126-
if (status < 0) {
127-
mp_raise_BrokenPipeError();
128-
}
129138
return received;
130139
}
131140

0 commit comments

Comments
 (0)