Skip to content

Commit 4f64f6b

Browse files
dpgeorgepfalcon
authored andcommitted
extmod/modlwip: Still process remaining incoming data of a closed socket.
It can happen that a socket gets closed while the pbuf is not completely drained by the application. It can also happen that a new pbuf comes in via the recv callback, and then a "peer closed" event comes via the same callback (pbuf=NULL) before the previous event has been handled. In both cases the socket is closed but there is remaining data. This patch makes sure such data is passed to the application.
1 parent 6d2e9e7 commit 4f64f6b

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

extmod/modlwip.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,6 @@ STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
384384

385385
// Helper function for recv/recvfrom to handle TCP packets
386386
STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
387-
388-
if (socket->state == STATE_PEER_CLOSED) {
389-
return 0;
390-
}
391-
392387
if (socket->incoming.pbuf == NULL) {
393388
mp_uint_t start = mp_hal_ticks_ms();
394389
while (socket->state == STATE_CONNECTED && socket->incoming.pbuf == NULL) {
@@ -399,7 +394,10 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
399394
poll_sockets();
400395
}
401396
if (socket->state == STATE_PEER_CLOSED) {
402-
return 0;
397+
if (socket->incoming.pbuf == NULL) {
398+
// socket closed and no data left in buffer
399+
return 0;
400+
}
403401
} else if (socket->state != STATE_CONNECTED) {
404402
*_errno = -socket->state;
405403
return -1;

0 commit comments

Comments
 (0)