@@ -515,7 +515,7 @@ nc_server_init(struct ly_ctx *ctx)
515515 server_opts .ctx = ctx ;
516516
517517 server_opts .new_session_id = 1 ;
518- pthread_spin_init ( & server_opts .sid_lock , PTHREAD_PROCESS_PRIVATE ) ;
518+ server_opts .new_client_id = 1 ;
519519
520520 errno = 0 ;
521521
@@ -549,8 +549,6 @@ nc_server_destroy(void)
549549 server_opts .capabilities = NULL ;
550550 server_opts .capabilities_count = 0 ;
551551
552- pthread_spin_destroy (& server_opts .sid_lock );
553-
554552#if defined(NC_ENABLED_SSH ) || defined(NC_ENABLED_TLS )
555553 nc_server_del_endpt (NULL , 0 );
556554#endif
@@ -703,9 +701,7 @@ nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **s
703701 (* session )-> ctx = server_opts .ctx ;
704702
705703 /* assign new SID atomically */
706- pthread_spin_lock (& server_opts .sid_lock );
707- (* session )-> id = server_opts .new_session_id ++ ;
708- pthread_spin_unlock (& server_opts .sid_lock );
704+ (* session )-> id = atomic_fetch_add (& server_opts .new_session_id , 1 );
709705
710706 /* NETCONF handshake */
711707 msgtype = nc_handshake_io (* session );
@@ -2015,11 +2011,7 @@ nc_accept(int timeout, struct nc_session **session)
20152011 pthread_rwlock_unlock (& server_opts .endpt_lock );
20162012
20172013 /* assign new SID atomically */
2018- /* LOCK */
2019- pthread_spin_lock (& server_opts .sid_lock );
2020- (* session )-> id = server_opts .new_session_id ++ ;
2021- /* UNLOCK */
2022- pthread_spin_unlock (& server_opts .sid_lock );
2014+ (* session )-> id = atomic_fetch_add (& server_opts .new_session_id , 1 );
20232015
20242016 /* NETCONF handshake */
20252017 msgtype = nc_handshake_io (* session );
@@ -2081,6 +2073,7 @@ nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti)
20812073 return -1 ;
20822074 }
20832075 server_opts .ch_clients [server_opts .ch_client_count - 1 ].name = lydict_insert (server_opts .ctx , name , 0 );
2076+ server_opts .ch_clients [server_opts .ch_client_count - 1 ].id = atomic_fetch_add (& server_opts .new_client_id , 1 );
20842077 server_opts .ch_clients [server_opts .ch_client_count - 1 ].ti = ti ;
20852078 server_opts .ch_clients [server_opts .ch_client_count - 1 ].ch_endpts = NULL ;
20862079 server_opts .ch_clients [server_opts .ch_client_count - 1 ].ch_endpt_count = 0 ;
@@ -2758,11 +2751,7 @@ nc_connect_ch_client_endpt(struct nc_ch_client *client, struct nc_ch_endpt *endp
27582751 }
27592752
27602753 /* assign new SID atomically */
2761- /* LOCK */
2762- pthread_spin_lock (& server_opts .sid_lock );
2763- (* session )-> id = server_opts .new_session_id ++ ;
2764- /* UNLOCK */
2765- pthread_spin_unlock (& server_opts .sid_lock );
2754+ (* session )-> id = atomic_fetch_add (& server_opts .new_session_id , 1 );
27662755
27672756 /* NETCONF handshake */
27682757 msgtype = nc_handshake_io (* session );
@@ -2909,12 +2898,14 @@ nc_ch_client_thread(void *arg)
29092898 struct nc_ch_endpt * cur_endpt ;
29102899 struct nc_session * session ;
29112900 struct nc_ch_client * client ;
2901+ uint32_t client_id ;
29122902
29132903 /* LOCK */
29142904 client = nc_server_ch_client_with_endpt_lock (data -> client_name );
29152905 if (!client ) {
29162906 goto cleanup ;
29172907 }
2908+ client_id = client -> id ;
29182909
29192910 cur_endpt = & client -> ch_endpts [0 ];
29202911 cur_endpt_name = strdup (cur_endpt -> name );
@@ -2938,6 +2929,10 @@ nc_ch_client_thread(void *arg)
29382929 if (!client ) {
29392930 goto cleanup ;
29402931 }
2932+ if (client -> id != client_id ) {
2933+ nc_server_ch_client_unlock (client );
2934+ goto cleanup ;
2935+ }
29412936
29422937 /* session changed status -> it was disconnected for whatever reason,
29432938 * persistent connection immediately tries to reconnect, periodic waits some first */
@@ -2953,6 +2948,10 @@ nc_ch_client_thread(void *arg)
29532948 if (!client ) {
29542949 goto cleanup ;
29552950 }
2951+ if (client -> id != client_id ) {
2952+ nc_server_ch_client_unlock (client );
2953+ goto cleanup ;
2954+ }
29562955 }
29572956
29582957 /* set next endpoint to try */
@@ -2983,6 +2982,10 @@ nc_ch_client_thread(void *arg)
29832982 if (!client ) {
29842983 goto cleanup ;
29852984 }
2985+ if (client -> id != client_id ) {
2986+ nc_server_ch_client_unlock (client );
2987+ goto cleanup ;
2988+ }
29862989
29872990 ++ cur_attempts ;
29882991
0 commit comments