Skip to content

Commit 6d2e9e7

Browse files
dpgeorgepfalcon
authored andcommitted
extmod/modlwip: Check for state change during recv busy-wait loop.
For example, the peer may close the connection while recv is waiting for incoming data.
1 parent 6185dc5 commit 6d2e9e7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

extmod/modlwip.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,19 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
391391

392392
if (socket->incoming.pbuf == NULL) {
393393
mp_uint_t start = mp_hal_ticks_ms();
394-
while (socket->incoming.pbuf == NULL) {
394+
while (socket->state == STATE_CONNECTED && socket->incoming.pbuf == NULL) {
395395
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
396396
*_errno = ETIMEDOUT;
397397
return -1;
398398
}
399399
poll_sockets();
400400
}
401+
if (socket->state == STATE_PEER_CLOSED) {
402+
return 0;
403+
} else if (socket->state != STATE_CONNECTED) {
404+
*_errno = -socket->state;
405+
return -1;
406+
}
401407
}
402408

403409
struct pbuf *p = socket->incoming.pbuf;

0 commit comments

Comments
 (0)