Skip to content

Commit

Permalink
net: tcp2: Properly cleanup receive queue
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jukkar authored and nashif committed Feb 1, 2021
1 parent de72fae commit 4c8760b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions subsys/net/ip/tcp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,13 @@ static int tcp_conn_unref(struct tcp *conn)

/* If there is any pending data, pass that to application */
while ((pkt = k_fifo_get(&conn->recv_data, K_NO_WAIT)) != NULL) {
net_context_packet_received(
(struct net_conn *)conn->context->conn_handler,
pkt, NULL, NULL, conn->recv_user_data);
if (net_context_packet_received(
(struct net_conn *)conn->context->conn_handler,
pkt, NULL, NULL, conn->recv_user_data) ==
NET_DROP) {
/* Application is no longer there, unref the pkt */
tcp_pkt_unref(pkt);
}
}

if (conn->context->conn_handler) {
Expand Down Expand Up @@ -1816,8 +1820,12 @@ static void tcp_in(struct tcp *conn, struct net_pkt *pkt)
*/
while (conn_handler && atomic_get(&conn->ref_count) > 0 &&
(recv_pkt = k_fifo_get(recv_data_fifo, K_NO_WAIT)) != NULL) {
net_context_packet_received(conn_handler, recv_pkt, NULL, NULL,
recv_user_data);
if (net_context_packet_received(conn_handler, recv_pkt, NULL,
NULL, recv_user_data) ==
NET_DROP) {
/* Application is no longer there, unref the pkt */
tcp_pkt_unref(recv_pkt);
}
}

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

0 comments on commit 4c8760b

Please sign in to comment.