Skip to content

Commit eab2869

Browse files
committed
extmod/modlwip: Don't allow writing to a TCP socket that is connecting.
This follows the behaviour of unix MicroPython (POSIX sockets) and the esp32 port. Signed-off-by: Damien George <[email protected]>
1 parent 3844733 commit eab2869

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

extmod/modlwip.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,12 @@ static mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
701701

702702
MICROPY_PY_LWIP_ENTER
703703

704-
u16_t available = tcp_sndbuf(socket->pcb.tcp);
704+
// If the socket is still connecting then don't let data be written to it.
705+
// Otherwise, get the number of available bytes in the output buffer.
706+
u16_t available = 0;
707+
if (socket->state != STATE_CONNECTING) {
708+
available = tcp_sndbuf(socket->pcb.tcp);
709+
}
705710

706711
if (available == 0) {
707712
// Non-blocking socket
@@ -718,7 +723,8 @@ static mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
718723
// If peer fully closed socket, we would have socket->state set to ERR_RST (connection
719724
// reset) by error callback.
720725
// Avoid sending too small packets, so wait until at least 16 bytes available
721-
while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) {
726+
while (socket->state == STATE_CONNECTING
727+
|| (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16)) {
722728
MICROPY_PY_LWIP_EXIT
723729
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
724730
*_errno = MP_ETIMEDOUT;
@@ -1548,7 +1554,7 @@ static mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
15481554
// raw socket is writable
15491555
ret |= MP_STREAM_POLL_WR;
15501556
#endif
1551-
} else if (socket->pcb.tcp != NULL && tcp_sndbuf(socket->pcb.tcp) > 0) {
1557+
} else if (socket->state != STATE_CONNECTING && socket->pcb.tcp != NULL && tcp_sndbuf(socket->pcb.tcp) > 0) {
15521558
// TCP socket is writable
15531559
// Note: pcb.tcp==NULL if state<0, and in this case we can't call tcp_sndbuf
15541560
ret |= MP_STREAM_POLL_WR;

0 commit comments

Comments
 (0)