diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c index 26937bd0b9997..3126da3dcc329 100644 --- a/subsys/net/ip/net_context.c +++ b/subsys/net/ip/net_context.c @@ -553,6 +553,13 @@ int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t proto, k_sem_give(&contexts_lock); if (ret < 0) { + if (ret == -EADDRINUSE && + !net_if_is_ip_offloaded(net_if_get_default()) && + proto == IPPROTO_TCP) { + /* Free the TCP context that we allocated earlier */ + net_tcp_put(&contexts[i]); + } + return ret; }