Skip to content

Commit 2d49a4c

Browse files
Tomasz Bursztykacfriedt
authored andcommitted
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 <[email protected]>
1 parent 9d229e0 commit 2d49a4c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

subsys/net/ip/net_context.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,12 @@ int net_context_put(struct net_context *context)
411411
context->recv_cb = NULL;
412412
context->send_cb = NULL;
413413

414-
/* Decrement refcount on user app's behalf */
415-
net_context_unref(context);
416-
417414
/* net_tcp_put() will handle decrementing refcount on stack's behalf */
418415
net_tcp_put(context);
419416

417+
/* Decrement refcount on user app's behalf */
418+
net_context_unref(context);
419+
420420
unlock:
421421
k_mutex_unlock(&context->lock);
422422

0 commit comments

Comments
 (0)