From 7998d5bb06be9dd3ffe79c63d4f1206d97dea080 Mon Sep 17 00:00:00 2001
From: Dmitriy Gerasimov <naeper@demlabs.net>
Date: Fri, 16 Jul 2021 17:59:49 +0700
Subject: [PATCH] [*] Replaced rdlocks on wrlocks

---
 CMakeLists.txt                                        | 2 +-
 dap-sdk/net/core/dap_events_socket.c                  | 7 ++++++-
 modules/global-db/dap_chain_global_db_driver_sqlite.c | 6 +++---
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index babad017c9..7553c4bc2c 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-9")
+set(CELLFRAME_SDK_NATIVE_VERSION "2.9-10")
 add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
 set(DAPSDK_MODULES "")
 message("Cellframe modules: ${CELLFRAME_MODULES}")
diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c
index fbdf968b1f..de5d8742a2 100644
--- a/dap-sdk/net/core/dap_events_socket.c
+++ b/dap-sdk/net/core/dap_events_socket.c
@@ -1548,7 +1548,12 @@ void dap_events_socket_worker_poll_update_unsafe(dap_events_socket_t * a_esocket
                 }
             }
         }
-        if ( l_is_error && l_errno != EINPROGRESS && l_errno != ENOENT){
+        if (l_is_error && l_errno == EBADF){
+            log_it(L_ATT,"Socket %d (%p ) disconnected, rise CLOSE flag to remove from queue, lost %"DAP_UINT64_FORMAT_u":%" DAP_UINT64_FORMAT_u
+                         " bytes",a_esocket->socket,a_esocket,a_esocket->buf_in_size,a_esocket->buf_out_size);
+            a_esocket->flags |= DAP_SOCK_SIGNAL_CLOSE;
+            a_esocket->buf_in_size = a_esocket->buf_out_size = 0; // Reset everything from buffer, we close it now all
+        }else if ( l_is_error && l_errno != EINPROGRESS && l_errno != ENOENT){
             char l_errbuf[128];
             l_errbuf[0]=0;
             strerror_r(l_errno, l_errbuf, sizeof (l_errbuf));
diff --git a/modules/global-db/dap_chain_global_db_driver_sqlite.c b/modules/global-db/dap_chain_global_db_driver_sqlite.c
index c101cbe340..43a2e07297 100644
--- a/modules/global-db/dap_chain_global_db_driver_sqlite.c
+++ b/modules/global-db/dap_chain_global_db_driver_sqlite.c
@@ -671,7 +671,7 @@ dap_store_obj_t* dap_db_driver_sqlite_read_last_store_obj(const char *a_group)
         return NULL;
     char * l_table_name = dap_db_driver_sqlite_make_table_name(a_group);
     char *l_str_query = sqlite3_mprintf("SELECT id,ts,key,value FROM '%s' ORDER BY id DESC LIMIT 1", l_table_name);
-    pthread_rwlock_rdlock(&s_db_rwlock);
+    pthread_rwlock_wrlock(&s_db_rwlock);
     if(!s_db){
         pthread_rwlock_unlock(&s_db_rwlock);
         return NULL;
@@ -731,7 +731,7 @@ dap_store_obj_t* dap_db_driver_sqlite_read_cond_store_obj(const char *a_group, u
     else
         l_str_query = sqlite3_mprintf("SELECT id,ts,key,value FROM '%s' WHERE id>'%lld' ORDER BY id ASC",
                 l_table_name, a_id);
-    pthread_rwlock_rdlock(&s_db_rwlock);
+    pthread_rwlock_wrlock(&s_db_rwlock);
     if(!s_db){
         pthread_rwlock_unlock(&s_db_rwlock);
         return NULL;
@@ -817,7 +817,7 @@ dap_store_obj_t* dap_db_driver_sqlite_read_store_obj(const char *a_group, const
         else
             l_str_query = sqlite3_mprintf("SELECT id,ts,key,value FROM '%s' ORDER BY id ASC", l_table_name);
     }
-    pthread_rwlock_rdlock(&s_db_rwlock);
+    pthread_rwlock_wrlock(&s_db_rwlock);
     if(!s_db){
         pthread_rwlock_unlock(&s_db_rwlock);
         return NULL;
-- 
GitLab