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.
This commit is contained in:
Damien George 2015-12-30 19:03:58 +00:00 committed by Paul Sokolovsky
parent 6185dc5f3d
commit 6d2e9e70b3
1 changed files with 7 additions and 1 deletions

View File

@ -391,13 +391,19 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
if (socket->incoming.pbuf == NULL) { if (socket->incoming.pbuf == NULL) {
mp_uint_t start = mp_hal_ticks_ms(); mp_uint_t start = mp_hal_ticks_ms();
while (socket->incoming.pbuf == NULL) { while (socket->state == STATE_CONNECTED && socket->incoming.pbuf == NULL) {
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) { if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
*_errno = ETIMEDOUT; *_errno = ETIMEDOUT;
return -1; return -1;
} }
poll_sockets(); poll_sockets();
} }
if (socket->state == STATE_PEER_CLOSED) {
return 0;
} else if (socket->state != STATE_CONNECTED) {
*_errno = -socket->state;
return -1;
}
} }
struct pbuf *p = socket->incoming.pbuf; struct pbuf *p = socket->incoming.pbuf;