From 25b1a108529c55a43ccefed22dab783a87b1a407 Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Wed, 25 Aug 2021 08:10:56 +0000
Subject: [PATCH] bugs-4850

---
 CMakeLists.txt                                |   2 +-
 dap-sdk/net/client/dap_client.c               |  42 +++----
 dap-sdk/net/client/dap_client_pvt.c           | 112 ++++++++++--------
 dap-sdk/net/client/include/dap_client.h       |   1 +
 modules/channel/chain/dap_stream_ch_chain.c   |   2 +-
 modules/net/dap_chain_node_client.c           |  33 +++---
 .../service/vpn/dap_chain_net_vpn_client.c    |   5 +-
 7 files changed, 103 insertions(+), 94 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e7ae039b5f..5b229c1d6a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,7 @@ project(cellframe-sdk C)
 cmake_minimum_required(VERSION 3.0)
 
 set(CMAKE_C_STANDARD 11)
-set(CELLFRAME_SDK_NATIVE_VERSION "2.9-29")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.9-30")
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 set(DAPSDK_MODULES "")
 message("Cellframe modules: ${CELLFRAME_MODULES}")
diff --git a/dap-sdk/net/client/dap_client.c b/dap-sdk/net/client/dap_client.c
index 138c188502..d992fe2d8e 100644
--- a/dap-sdk/net/client/dap_client.c
+++ b/dap-sdk/net/client/dap_client.c
@@ -86,13 +86,15 @@ dap_client_t * dap_client_new(dap_events_t * a_events, dap_client_callback_t a_s
         goto MEM_ALLOC_ERR;
 
     // CONSTRUCT dap_client object
-    DAP_CLIENT_PVT(l_client)->client = l_client;
-    DAP_CLIENT_PVT(l_client)->events = a_events;
-    DAP_CLIENT_PVT(l_client)->stage_status_callback = a_stage_status_callback;
-    DAP_CLIENT_PVT(l_client)->stage_status_error_callback = a_stage_status_error_callback;
-    DAP_CLIENT_PVT(l_client)->worker = dap_events_worker_get_auto();
+    dap_client_pvt_t *l_client_pvt = DAP_CLIENT_PVT(l_client);
+    l_client_pvt->client = l_client;
+    l_client_pvt->events = a_events;
+    l_client_pvt->stage_status_callback = a_stage_status_callback;
+    l_client_pvt->stage_status_error_callback = a_stage_status_error_callback;
+    l_client_pvt->worker = dap_events_worker_get_auto();
 
-    dap_client_pvt_new(DAP_CLIENT_PVT(l_client) );
+    dap_client_pvt_new(l_client_pvt);
+    l_client->pvt_uuid = l_client_pvt->uuid;
 
     return l_client;
 
@@ -235,8 +237,8 @@ static void s_go_stage_on_client_worker_unsafe(dap_worker_t * a_worker,void * a_
     DAP_DELETE(a_arg);
 
     l_client_pvt->is_to_delete = l_flag_delete_after;
-    if ( l_client==NULL){
-        log_it(L_WARNING,"Client is NULL, why? Refs %u", l_client_pvt->refs_count);
+    if (!l_client || l_client->_internal != l_client_pvt) {
+        log_it(L_WARNING,"Client is NULL or corrupted, why? Refs %u", l_client_pvt->refs_count);
         if ( l_client_pvt->refs_count ==0 ){
             dap_client_pvt_delete( l_client_pvt );
         } else
@@ -317,7 +319,7 @@ void dap_client_go_stage(dap_client_t * a_client, dap_client_stage_t a_stage_tar
         log_it(L_ERROR, "dap_client_go_stage, a_stage_end_callback == NULL");
         return;
     }
-    dap_client_pvt_t * l_client_pvt = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_pvt = dap_client_pvt_find(a_client->pvt_uuid);
 
     assert(l_client_pvt);
 
@@ -335,7 +337,7 @@ void dap_client_go_stage(dap_client_t * a_client, dap_client_stage_t a_stage_tar
 void dap_client_delete_mt(dap_client_t * a_client)
 {
     assert(a_client);
-    dap_client_pvt_t * l_client_pvt = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_pvt = dap_client_pvt_find(a_client->pvt_uuid);
     assert(l_client_pvt);
 
     struct go_stage_arg *l_stage_arg = DAP_NEW(struct go_stage_arg); if (! l_stage_arg) return;
@@ -356,7 +358,7 @@ static void s_stage_fsm_operator_unsafe(dap_client_t * a_client, void * a_arg)
 {
     UNUSED(a_arg);
     assert(a_client);
-    dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_internal = dap_client_pvt_find(a_client->pvt_uuid);
     assert(l_client_internal);
 
     if ( l_client_internal->is_to_delete ){ // If we're switched once to delete and smbd else switched to another state - we restore target
@@ -398,14 +400,14 @@ static void s_stage_fsm_operator_unsafe(dap_client_t * a_client, void * a_arg)
 void dap_client_request_enc_unsafe(dap_client_t * a_client, const char * a_path, const char * a_suburl,const char* a_query, void * a_request, size_t a_request_size,
                                 dap_client_callback_data_size_t a_response_proc, dap_client_callback_int_t a_response_error )
 {
-    dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_internal = dap_client_pvt_find(a_client->pvt_uuid);
     dap_client_pvt_request_enc(l_client_internal, a_path, a_suburl, a_query,a_request,a_request_size, a_response_proc,a_response_error);
 }
 
 void dap_client_request_unsafe(dap_client_t * a_client, const char * a_full_path, void * a_request, size_t a_request_size,
                                 dap_client_callback_data_size_t a_response_proc, dap_client_callback_int_t a_response_error )
 {
-    dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_internal = dap_client_pvt_find(a_client->pvt_uuid);
     dap_client_pvt_request(l_client_internal, a_full_path, a_request, a_request_size, a_response_proc, a_response_error);
 }
 
@@ -439,7 +441,7 @@ const char * dap_client_error_str(dap_client_error_t a_client_error)
  */
 const char * dap_client_get_error_str(dap_client_t * a_client)
 {
-    if(a_client == NULL || DAP_CLIENT_PVT(a_client) == NULL){
+    if(a_client == NULL || dap_client_pvt_find(a_client->pvt_uuid) == NULL) {
         log_it(L_ERROR,"Client is NULL for dap_client_get_error_str");
         return NULL;
     }
@@ -527,7 +529,7 @@ const char * dap_client_stage_str(dap_client_stage_t a_stage)
  */
 dap_client_stage_status_t dap_client_get_stage_status(dap_client_t * a_client)
 {
-    return (a_client && DAP_CLIENT_PVT(a_client)) ? DAP_CLIENT_PVT(a_client)->stage_status : STAGE_STATUS_NONE;
+    return (a_client && dap_client_pvt_find(a_client->pvt_uuid)) ? DAP_CLIENT_PVT(a_client)->stage_status : STAGE_STATUS_NONE;
 }
 
 /**
@@ -536,7 +538,7 @@ dap_client_stage_status_t dap_client_get_stage_status(dap_client_t * a_client)
  * @return
  */
 dap_enc_key_t * dap_client_get_key_stream(dap_client_t * a_client){
-    return (a_client && DAP_CLIENT_PVT(a_client)) ? DAP_CLIENT_PVT(a_client)->stream_key : NULL;
+    return (a_client && dap_client_pvt_find(a_client->pvt_uuid)) ? DAP_CLIENT_PVT(a_client)->stream_key : NULL;
 }
 
 
@@ -552,7 +554,7 @@ dap_stream_t * dap_client_get_stream(dap_client_t * a_client)
         return NULL;
     }
 
-    dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_internal = dap_client_pvt_find(a_client->pvt_uuid);
     return (l_client_internal) ? l_client_internal->stream : NULL;
 }
 
@@ -567,7 +569,7 @@ dap_stream_worker_t * dap_client_get_stream_worker(dap_client_t * a_client)
         log_it(L_ERROR,"Client is NULL for dap_client_get_stream_worker");
         return NULL;
     }
-    dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(a_client);
+    dap_client_pvt_t * l_client_internal = dap_client_pvt_find(a_client->pvt_uuid);
     return (l_client_internal) ? l_client_internal->stream_worker : NULL;
 
 }
@@ -575,7 +577,7 @@ dap_stream_worker_t * dap_client_get_stream_worker(dap_client_t * a_client)
 dap_stream_ch_t * dap_client_get_stream_ch_unsafe(dap_client_t * a_client, uint8_t a_ch_id)
 {
     dap_stream_ch_t * l_ch = NULL;
-    dap_client_pvt_t * l_client_internal = a_client ? DAP_CLIENT_PVT(a_client) : NULL;
+    dap_client_pvt_t * l_client_internal = a_client ? dap_client_pvt_find(a_client->pvt_uuid) : NULL;
     if(l_client_internal && l_client_internal->stream && l_client_internal->stream_es)
         for(size_t i = 0; i < l_client_internal->stream->channel_count; i++) {
             if(l_client_internal->stream->channel[i]->proc->id == a_ch_id) {
@@ -593,7 +595,7 @@ dap_stream_ch_t * dap_client_get_stream_ch_unsafe(dap_client_t * a_client, uint8
  */
 const char * dap_client_get_stream_id(dap_client_t * a_client)
 {
-    if(!(a_client || !DAP_CLIENT_PVT(a_client)))
+    if(!(a_client || !dap_client_pvt_find(a_client->pvt_uuid)))
         return NULL;
     return DAP_CLIENT_PVT(a_client)->stream_id;
 }
diff --git a/dap-sdk/net/client/dap_client_pvt.c b/dap-sdk/net/client/dap_client_pvt.c
index 537221e0b2..58349cc6a5 100644
--- a/dap-sdk/net/client/dap_client_pvt.c
+++ b/dap-sdk/net/client/dap_client_pvt.c
@@ -167,14 +167,19 @@ void dap_client_pvt_new(dap_client_pvt_t * a_client_pvt)
 void dap_client_pvt_delete(dap_client_pvt_t * a_client_pvt)
 {
     assert(a_client_pvt);
-    if(a_client_pvt->delete_callback)
-        a_client_pvt->delete_callback(a_client_pvt->client, NULL);
-    // delete from list
-    if(dap_client_pvt_hh_del_unsafe(a_client_pvt)<0){
+
+    if (!dap_client_pvt_find(a_client_pvt->uuid)) {
         if(s_debug_more)
             log_it(L_DEBUG, "dap_client_pvt 0x%x already deleted", a_client_pvt);
         return;
     }
+    if(a_client_pvt->delete_callback)
+        a_client_pvt->delete_callback(a_client_pvt->client, NULL);
+    if (a_client_pvt->stream_es) {
+        dap_events_socket_remove_and_delete_unsafe(a_client_pvt->stream_es, true);
+    }
+    // delete from list
+    dap_client_pvt_hh_del_unsafe(a_client_pvt);
     if(s_debug_more)
         log_it(L_INFO, "dap_client_pvt_delete 0x%x", a_client_pvt);
 
@@ -233,17 +238,21 @@ static bool s_stream_timer_timeout_check(void * a_arg)
     dap_events_socket_t * l_es = dap_worker_esocket_find_uuid(l_worker, *l_es_uuid_ptr);
     if(l_es){
         if (l_es->flags & DAP_SOCK_CONNECTING ){
-            dap_client_pvt_t * l_client_pvt =(dap_client_pvt_t *) l_es->_inheritor;//(l_client) ? DAP_CLIENT_PVT(l_client) : NULL;
-
-            log_it(L_WARNING,"Connecting timeout for stream uplink request http://%s:%u/, possible network problems or host is down",
-                   l_client_pvt->uplink_addr, l_client_pvt->uplink_port);
-            l_client_pvt->is_closed_by_timeout = true;
-            if(l_es->callbacks.error_callback) {
-                l_es->callbacks.error_callback(l_es,ETIMEDOUT);
+            dap_client_pvt_t * l_client_pvt = (dap_client_pvt_t *)l_es->_inheritor;
+            if (dap_client_pvt_find(l_client_pvt->uuid)) {
+                log_it(L_WARNING,"Connecting timeout for stream uplink request http://%s:%u/, possible network problems or host is down",
+                       l_client_pvt->uplink_addr, l_client_pvt->uplink_port);
+                l_client_pvt->is_closed_by_timeout = true;
+                if(l_es->callbacks.error_callback) {
+                    l_es->callbacks.error_callback(l_es,ETIMEDOUT);
+                }
+                log_it(L_INFO, "Close %s sock %u type %d by timeout",
+                       l_es->remote_addr_str ? l_es->remote_addr_str : "", l_es->socket, l_es->type);
+                dap_client_delete_unsafe(l_client_pvt->client);
+            } else {
+                log_it(L_ERROR,"Connecting timeout for unexistent client");
+                dap_events_socket_remove_and_delete_unsafe(l_es,true);
             }
-            log_it(L_INFO, "Close %s sock %u type %d by timeout",
-                   l_es->remote_addr_str ? l_es->remote_addr_str : "", l_es->socket, l_es->type);
-            dap_events_socket_remove_and_delete_unsafe(l_es,true);
         }else
             if(s_debug_more)
                 log_it(L_DEBUG,"Socket %d is connected, close check timer", l_es->socket);
@@ -270,21 +279,27 @@ static bool s_stream_timer_timeout_after_connected_check(void * a_arg)
 
     dap_events_socket_t * l_es = dap_worker_esocket_find_uuid(l_worker, *l_es_uuid_ptr);
     if( l_es ){
-        dap_client_pvt_t * l_client_pvt =(dap_client_pvt_t *) l_es->_inheritor;//(l_client) ? DAP_CLIENT_PVT(l_client) : NULL;
-        if ( time(NULL)- l_client_pvt->ts_last_read >= s_client_timeout_read_after_connect_seconds){
-
-            log_it(L_WARNING,"Connecting timeout for streaming uplink http://%s:%u/, possible network problems or host is down",
-                   l_client_pvt->uplink_addr, l_client_pvt->uplink_port);
-            l_client_pvt->is_closed_by_timeout = true;
-            if(l_es->callbacks.error_callback) {
-                l_es->callbacks.error_callback(l_es,ETIMEDOUT);
-            }
-            log_it(L_INFO, "Close streaming socket %s by timeout",
-                   l_es->remote_addr_str ? l_es->remote_addr_str : "", l_es->socket);
+        dap_client_pvt_t * l_client_pvt = (dap_client_pvt_t *)l_es->_inheritor;
+        if (dap_client_pvt_find(l_client_pvt->uuid)) {
+            if ( time(NULL)- l_client_pvt->ts_last_read >= s_client_timeout_read_after_connect_seconds){
+
+                log_it(L_WARNING,"Activity timeout for streaming uplink http://%s:%u/, possible network problems or host is down",
+                       l_client_pvt->uplink_addr, l_client_pvt->uplink_port);
+                l_client_pvt->is_closed_by_timeout = true;
+                if(l_es->callbacks.error_callback) {
+                    l_es->callbacks.error_callback(l_es,ETIMEDOUT);
+                }
+                log_it(L_INFO, "Close streaming socket %s by timeout",
+                       l_es->remote_addr_str ? l_es->remote_addr_str : "", l_es->socket);
+                dap_client_delete_unsafe(l_client_pvt->client);
+            }else
+                if(s_debug_more)
+                    log_it(L_DEBUG,"Streaming socket %d is connected, close check timer", l_es->socket);
+        } else {
+            log_it(L_ERROR,"Activity timeout for unexistent client");
             dap_events_socket_remove_and_delete_unsafe(l_es,true);
-        }else
-            if(s_debug_more)
-                log_it(L_DEBUG,"Streaming socket %d is connected, close check timer", l_es->socket);
+        }
+
     }else
         if(s_debug_more)
             log_it(L_DEBUG,"Streaming socket %"DAP_UINT64_FORMAT_u" is finished, close check timer", *l_es_uuid_ptr);
@@ -319,6 +334,8 @@ static bool s_enc_init_delay_before_request_timer_callback(void * a_arg)
  */
 static bool s_stage_status_after(dap_client_pvt_t * a_client_pvt)
 {
+    if (!dap_client_pvt_find(a_client_pvt->uuid))
+        return false;
     dap_worker_t * l_worker= a_client_pvt->worker;
     assert(l_worker);
     assert(l_worker->_inheritor);
@@ -647,7 +664,7 @@ static bool s_stage_status_after(dap_client_pvt_t * a_client_pvt)
                             log_it(L_ERROR,"Can't run timer for bigger delay before the next enc_init request");
                         }
                     } else {
-                        log_it(L_ERROR, "Connet to %s:%u failed", a_client_pvt->uplink_addr, a_client_pvt->uplink_port);
+                        log_it(L_ERROR, "Connect to %s:%u failed", a_client_pvt->uplink_addr, a_client_pvt->uplink_port);
                         dap_client_delete_mt(a_client_pvt->client);
                     }
                 }
@@ -870,8 +887,9 @@ static void s_request_error(int a_err_code, void * a_obj)
 
     if(l_client_pvt && l_client_pvt->request_error_callback && l_client_pvt->client)
     {
+        l_client_pvt = dap_client_pvt_find(l_client_pvt->uuid);
         if(l_client_pvt && l_client_pvt->request_error_callback
-                && l_client_pvt->client && l_client_pvt->client->_internal && l_client_pvt->client->_internal == l_client_pvt)
+                && l_client_pvt->client && l_client_pvt->client->_internal)
             l_client_pvt->request_error_callback(l_client_pvt->client, a_err_code);
     }
 }
@@ -921,8 +939,8 @@ static void s_request_response(void * a_response, size_t a_response_size, void *
  */
 static void s_enc_init_response(dap_client_t * a_client, void * a_response, size_t a_response_size)
 {
-    dap_client_pvt_t * l_client_pvt = DAP_CLIENT_PVT(a_client);
-    assert(l_client_pvt);
+    dap_client_pvt_t * l_client_pvt = dap_client_pvt_find(a_client->pvt_uuid);
+    if (!l_client_pvt) return;
 
     if (!l_client_pvt->session_key_open){
         log_it(L_ERROR, "m_enc_init_response: session is NULL!");
@@ -1037,9 +1055,9 @@ static void s_enc_init_response(dap_client_t * a_client, void * a_response, size
  */
 static void s_enc_init_error(dap_client_t * a_client, int a_err_code)
 {
-    dap_client_pvt_t * l_client_pvt = DAP_CLIENT_PVT(a_client);
-    assert(l_client_pvt);
+    dap_client_pvt_t * l_client_pvt = dap_client_pvt_find(a_client->pvt_uuid);
     log_it(L_ERROR, "ENC: Can't init ecnryption session, err code %d", a_err_code);
+    if (!l_client_pvt) return;
     if (a_err_code == ETIMEDOUT) {
         l_client_pvt->last_error = ERROR_NETWORK_CONNECTION_TIMEOUT;
     } else {
@@ -1057,9 +1075,8 @@ static void s_enc_init_error(dap_client_t * a_client, int a_err_code)
  */
 static void s_stream_ctl_response(dap_client_t * a_client, void * a_data, size_t a_data_size)
 {
-    dap_client_pvt_t * l_client_pvt = DAP_CLIENT_PVT(a_client);
-    assert(l_client_pvt);
-
+    dap_client_pvt_t *l_client_pvt = dap_client_pvt_find(a_client->pvt_uuid);
+    if (!l_client_pvt) return;
     if(s_debug_more)
         log_it(L_DEBUG, "STREAM_CTL response %u bytes length recieved", a_data_size);
     char * l_response_str = DAP_NEW_Z_SIZE(char, a_data_size + 1);
@@ -1218,8 +1235,7 @@ static void s_stream_es_callback_delete(dap_events_socket_t *a_es, void *arg)
     (void) arg;
     log_it(L_INFO, "Stream delete callback");
 
-    dap_client_pvt_t * l_client_pvt =(dap_client_pvt_t*) a_es->_inheritor;
-
+    dap_client_pvt_t *l_client_pvt = (dap_client_pvt_t *)a_es->_inheritor;
     a_es->_inheritor = NULL; // To prevent delete in reactor
 
     if(l_client_pvt == NULL) {
@@ -1227,16 +1243,14 @@ static void s_stream_es_callback_delete(dap_events_socket_t *a_es, void *arg)
         return;
     }
 
+    if (!dap_client_pvt_find(l_client_pvt->uuid)) {
+        log_it(L_ERROR, "dap_client_pvt is corrupted");
+        return;
+    }
+
     if(s_debug_more)
         log_it(L_DEBUG, "Delete stream socket for client_pvt=0x%x", l_client_pvt);
 
-    if (l_client_pvt->stage_status_error_callback) {
-        if(l_client_pvt == l_client_pvt->client->_internal)
-            l_client_pvt->stage_status_error_callback(l_client_pvt->client, (void *) true);
-        else {
-            log_it(L_ERROR, "client_pvt->client=%x corrupted", l_client_pvt->client->_internal);
-        }
-    }
     dap_stream_delete(l_client_pvt->stream);
     if (l_client_pvt->stream_es) {
         DAP_DEL_Z(l_client_pvt->stream_es->remote_addr_str)
@@ -1254,8 +1268,7 @@ static void s_stream_es_callback_delete(dap_events_socket_t *a_es, void *arg)
 static void s_stream_es_callback_read(dap_events_socket_t * a_es, void * arg)
 {
     (void) arg;
-    //dap_client_t * l_client = DAP_CLIENT(a_es);
-    dap_client_pvt_t * l_client_pvt =(dap_client_pvt_t *) a_es->_inheritor;//(l_client) ? DAP_CLIENT_PVT(l_client) : NULL;
+    dap_client_pvt_t * l_client_pvt =(dap_client_pvt_t *) a_es->_inheritor;
 
     l_client_pvt->ts_last_read = time(NULL);
     switch (l_client_pvt->stage) {
@@ -1301,8 +1314,6 @@ static void s_stream_es_callback_read(dap_events_socket_t * a_es, void * arg)
 static void s_stream_es_callback_write(dap_events_socket_t * a_es, void * arg)
 {
     (void) arg;
-    //dap_client_t * l_client = DAP_CLIENT(a_es);
-    //dap_client_pvt_t * l_client_pvt = (l_client) ? DAP_CLIENT_PVT(l_client) : NULL;
     dap_client_pvt_t * l_client_pvt = a_es->_inheritor;
 
     if (l_client_pvt->stage_status == STAGE_STATUS_ERROR || !l_client_pvt->stream)
@@ -1338,7 +1349,6 @@ static void s_stream_es_callback_write(dap_events_socket_t * a_es, void * arg)
  */
 static void s_stream_es_callback_error(dap_events_socket_t * a_es, int a_error)
 {
-    a_es->flags |= DAP_SOCK_SIGNAL_CLOSE;
     dap_client_pvt_t *l_client_pvt = (dap_client_pvt_t *) a_es->_inheritor;
     if (!l_client_pvt)
         return;
diff --git a/dap-sdk/net/client/include/dap_client.h b/dap-sdk/net/client/include/dap_client.h
index d311f50ded..1c6920b1b6 100644
--- a/dap-sdk/net/client/include/dap_client.h
+++ b/dap-sdk/net/client/include/dap_client.h
@@ -77,6 +77,7 @@ typedef struct dap_client{
     pthread_mutex_t mutex;
     void * _internal;
     void * _inheritor;
+    uint64_t pvt_uuid;
 } dap_client_t;
 
 typedef void (*dap_client_callback_t) (dap_client_t *, void*);
diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c
index 6a8beefe1c..a567e3ac4f 100644
--- a/modules/channel/chain/dap_stream_ch_chain.c
+++ b/modules/channel/chain/dap_stream_ch_chain.c
@@ -1483,7 +1483,7 @@ void s_stream_ch_packet_out(dap_stream_ch_t* a_ch, void* a_arg)
                 dap_stream_ch_chain_pkt_write_unsafe(a_ch, DAP_STREAM_CH_CHAIN_PKT_TYPE_SYNCED_CHAINS,
                                                      l_ch_chain->request_hdr.net_id.uint64, l_ch_chain->request_hdr.chain_id.uint64,
                                                      l_ch_chain->request_hdr.cell_id.uint64, &l_request, sizeof(l_request));
-                log_it( L_INFO,"Synced: %"DAP_UINT64_FORMAT_U" atoms processed", l_ch_chain->stats_request_atoms_processed);
+                log_it( L_INFO,"Synced: %"DAP_UINT64_FORMAT_u" atoms processed", l_ch_chain->stats_request_atoms_processed);
                 dap_stream_ch_chain_go_idle(l_ch_chain);
                 if (l_ch_chain->callback_notify_packet_out)
                     l_ch_chain->callback_notify_packet_out(l_ch_chain, DAP_STREAM_CH_CHAIN_PKT_TYPE_SYNCED_CHAINS, NULL,
diff --git a/modules/net/dap_chain_node_client.c b/modules/net/dap_chain_node_client.c
index 0e1e012c0d..829e078c69 100644
--- a/modules/net/dap_chain_node_client.c
+++ b/modules/net/dap_chain_node_client.c
@@ -470,7 +470,7 @@ static void s_ch_chain_callback_notify_packet_out(dap_stream_ch_chain_t* a_ch_ch
     dap_chain_node_client_t * l_node_client = (dap_chain_node_client_t *) a_arg;
     assert(a_arg);
     dap_stream_ch_t * l_ch = NULL;
-    if((l_ch = dap_stream_ch_find_by_uuid_unsafe(l_node_client->stream_worker, l_node_client->ch_chain_uuid)) != NULL){
+    //if((l_ch = dap_stream_ch_find_by_uuid_unsafe(l_node_client->stream_worker, l_node_client->ch_chain_uuid)) != NULL){
         switch (a_pkt_type) {
             case DAP_STREAM_CH_CHAIN_PKT_TYPE_SYNCED_GLOBAL_DB: {
                 if(s_stream_ch_chain_debug_more)
@@ -483,7 +483,7 @@ static void s_ch_chain_callback_notify_packet_out(dap_stream_ch_chain_t* a_ch_ch
             default: {
             }
         }
-    }
+    //}
 }
 
 static int save_stat_to_database(dap_stream_ch_chain_net_srv_pkt_test_t *a_request, dap_chain_node_client_t * a_node_client)
@@ -687,16 +687,22 @@ void dap_chain_node_client_reset(dap_chain_node_client_t *a_client)
  */
 void dap_chain_node_client_close(dap_chain_node_client_t *a_client)
 {
-    if (a_client && a_client->client) { // block tryes to close twice
+    if (!a_client)
+        return;
+    dap_chain_node_client_handle_t * l_client_found = NULL;
+    HASH_FIND(hh,s_clients,&a_client->uuid,sizeof(a_client->uuid),l_client_found);
+    if (l_client_found) {
+        HASH_DEL(s_clients,l_client_found);
+        DAP_DELETE(l_client_found);
         char l_node_addr_str[INET_ADDRSTRLEN] = {};
         inet_ntop(AF_INET, &a_client->info->hdr.ext_addr_v4, l_node_addr_str, INET_ADDRSTRLEN);
         log_it(L_INFO, "Closing node client to uplink %s:%d", l_node_addr_str, a_client->info->hdr.ext_port);
         // clean client
-        DAP_CLIENT_PVT(a_client->client)->stage_status_error_callback = NULL;
-        a_client->client->_inheritor = NULL;
-        if (a_client->stream_worker)
-            dap_events_socket_remove_and_delete_mt(a_client->stream_worker->worker, a_client->esocket_uuid);
-        dap_client_delete_mt(a_client->client);
+        dap_client_pvt_t *l_client_pvt = dap_client_pvt_find(a_client->client->pvt_uuid);
+        if (l_client_pvt) {
+            dap_client_delete_mt(a_client->client);
+            a_client->client->_inheritor = NULL;
+        }
 #ifndef _WIN32
         pthread_cond_destroy(&a_client->wait_cond);
 #else
@@ -704,16 +710,9 @@ void dap_chain_node_client_close(dap_chain_node_client_t *a_client)
 #endif
         pthread_mutex_destroy(&a_client->wait_mutex);
         a_client->client = NULL;
-        dap_chain_node_client_handle_t * l_client_found = NULL;
-        HASH_FIND(hh,s_clients,&a_client->uuid,sizeof(a_client->uuid),l_client_found);
-        if (l_client_found){
-            HASH_DEL(s_clients,l_client_found);
-            DAP_DELETE(l_client_found);
-        }else{
-            log_it(L_WARNING, "Chain node client was removed from hash table before for some reasons");
-        }
-
         DAP_DELETE(a_client);
+    } else {
+        log_it(L_WARNING, "Chain node client was removed from hash table before for some reasons");
     }
 }
 
diff --git a/modules/service/vpn/dap_chain_net_vpn_client.c b/modules/service/vpn/dap_chain_net_vpn_client.c
index 4021249ad7..d7f3a18017 100644
--- a/modules/service/vpn/dap_chain_net_vpn_client.c
+++ b/modules/service/vpn/dap_chain_net_vpn_client.c
@@ -412,9 +412,8 @@ int dap_chain_net_vpn_client_check(dap_chain_net_t *a_net, const char *a_ipv4_st
     // default 10 sec = 10000 ms
     if(a_timeout_test_ms==-1)
         a_timeout_test_ms = 10000;
-    // default 5 sec = 5000 ms
-    int l_timeout_conn_ms = 25000;
 
+    int l_timeout_conn_ms = 10000;
     int l_ret = 0;
     if(!a_ipv4_str) // && !a_ipv6_str)
         return -1;
@@ -437,8 +436,6 @@ int dap_chain_net_vpn_client_check(dap_chain_net_t *a_net, const char *a_ipv4_st
     s_vpn_client = dap_chain_node_client_create_n_connect(a_net, s_node_info, l_active_channels, NULL, NULL);
     if(!s_vpn_client) {
         log_it(L_ERROR, "Can't connect to VPN server=%s:%d", a_ipv4_str, a_port);
-        // clean client struct
-        dap_chain_node_client_close(s_vpn_client);
         DAP_DELETE(s_node_info);
         s_node_info = NULL;
         return -2;
-- 
GitLab