Skip to content

Commit da6b607

Browse files
rlubosfabiobaltieri
authored andcommitted
net: tcp: Do not cancel connect sem from tcp_conn_unref()
Resetting the connect semaphore from tcp_conn_unref() prevents the ref/unref mechanism from being used freely when needed. Therefore, move the code responsible for resetting pending connection to tcp_conn_close() (called only when the connection is being closed on behalf of the TCP stack, for instance due to RST being received) and to net_tcp_put() (connection closed on behalf of the application). Signed-off-by: Robert Lubos <[email protected]>
1 parent a41f625 commit da6b607

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

subsys/net/ip/tcp.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -836,17 +836,6 @@ static int tcp_conn_unref(struct tcp *conn)
836836

837837
NET_DBG("conn: %p, ref_count=%d", conn, ref_count);
838838

839-
k_mutex_lock(&conn->lock, K_FOREVER);
840-
841-
#if !defined(CONFIG_NET_TEST_PROTOCOL)
842-
if (conn->in_connect) {
843-
conn->in_connect = false;
844-
k_sem_reset(&conn->connect_sem);
845-
}
846-
#endif /* CONFIG_NET_TEST_PROTOCOL */
847-
848-
k_mutex_unlock(&conn->lock);
849-
850839
ref_count = atomic_dec(&conn->ref_count) - 1;
851840
if (ref_count != 0) {
852841
tp_out(net_context_get_family(conn->context), conn->iface,
@@ -888,6 +877,9 @@ static int tcp_conn_close(struct tcp *conn, int status)
888877
/* Make sure the connect_cb is only called once. */
889878
conn->connect_cb = NULL;
890879
}
880+
881+
conn->in_connect = false;
882+
k_sem_reset(&conn->connect_sem);
891883
} else if (conn->context->recv_cb) {
892884
conn->context->recv_cb(conn->context, NULL, NULL, NULL,
893885
status, conn->recv_user_data);
@@ -3680,6 +3672,7 @@ int net_tcp_put(struct net_context *context)
36803672
}
36813673
} else if (conn && conn->in_connect) {
36823674
conn->in_connect = false;
3675+
k_sem_reset(&conn->connect_sem);
36833676
}
36843677

36853678
k_mutex_unlock(&conn->lock);

0 commit comments

Comments
 (0)