Skip to content

Commit 4c8760b

Browse files
jukkarnashif
authored andcommitted
net: tcp2: Properly cleanup receive queue
When pushing received data to the application, check that app was able to receive the data. If the application already closed the socket, then we must free the received net_pkt in order to avoid memory leak. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent de72fae commit 4c8760b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

subsys/net/ip/tcp2.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,13 @@ static int tcp_conn_unref(struct tcp *conn)
371371

372372
/* If there is any pending data, pass that to application */
373373
while ((pkt = k_fifo_get(&conn->recv_data, K_NO_WAIT)) != NULL) {
374-
net_context_packet_received(
375-
(struct net_conn *)conn->context->conn_handler,
376-
pkt, NULL, NULL, conn->recv_user_data);
374+
if (net_context_packet_received(
375+
(struct net_conn *)conn->context->conn_handler,
376+
pkt, NULL, NULL, conn->recv_user_data) ==
377+
NET_DROP) {
378+
/* Application is no longer there, unref the pkt */
379+
tcp_pkt_unref(pkt);
380+
}
377381
}
378382

379383
if (conn->context->conn_handler) {
@@ -1816,8 +1820,12 @@ static void tcp_in(struct tcp *conn, struct net_pkt *pkt)
18161820
*/
18171821
while (conn_handler && atomic_get(&conn->ref_count) > 0 &&
18181822
(recv_pkt = k_fifo_get(recv_data_fifo, K_NO_WAIT)) != NULL) {
1819-
net_context_packet_received(conn_handler, recv_pkt, NULL, NULL,
1820-
recv_user_data);
1823+
if (net_context_packet_received(conn_handler, recv_pkt, NULL,
1824+
NULL, recv_user_data) ==
1825+
NET_DROP) {
1826+
/* Application is no longer there, unref the pkt */
1827+
tcp_pkt_unref(recv_pkt);
1828+
}
18211829
}
18221830

18231831
/* We must not try to unref the connection while having a connection

0 commit comments

Comments
 (0)