From 8a2e5b16483888e46c9ccbd31d803841663f0b97 Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Thu, 16 Sep 2021 14:32:41 +0200 Subject: [PATCH] net/context: Close TCP connection properly Closing a connection, thus calling net_context_put() will not close a TCP connection properly, and will leak tcp connection memory. This is because: net_context_put calls net_context_unref which calls net_tcp_unref which leads to unref tcp connection and thus sets ctx->tcp to NULL. Back to net_context_put, that one finally calls net_tcp_put: but that bails out directly since ctx->tcp is NULL. Fixing it by inverting net_tcp_put() and net_context_unref() calls within net_context_put(). Fixes #38598 Signed-off-by: Tomasz Bursztyka --- subsys/net/ip/net_context.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c index 3c0e75d9538..c82304cce1d 100644 --- a/subsys/net/ip/net_context.c +++ b/subsys/net/ip/net_context.c @@ -411,12 +411,12 @@ int net_context_put(struct net_context *context) context->recv_cb = NULL; context->send_cb = NULL; - /* Decrement refcount on user app's behalf */ - net_context_unref(context); - /* net_tcp_put() will handle decrementing refcount on stack's behalf */ net_tcp_put(context); + /* Decrement refcount on user app's behalf */ + net_context_unref(context); + unlock: k_mutex_unlock(&context->lock);