From 7f1c7c9f174316b042635b8a96a1527a8e215eff Mon Sep 17 00:00:00 2001 From: LucioDonda Date: Tue, 3 Dec 2024 15:37:18 +0000 Subject: [PATCH] fix: move mutex to the places where m_spDBSync is used --- src/modules/inventory/include/inventory.hpp | 2 +- src/modules/inventory/src/inventoryImp.cpp | 53 +++++++++++++-------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/modules/inventory/include/inventory.hpp b/src/modules/inventory/include/inventory.hpp index 57e046542f..eccc6d9261 100644 --- a/src/modules/inventory/include/inventory.hpp +++ b/src/modules/inventory/include/inventory.hpp @@ -74,7 +74,7 @@ class Inventory { void ScanPorts(); void ScanProcesses(); void Scan(); - void SyncLoop(std::unique_lock& lock); + void SyncLoop(); void ShowConfig(); cJSON * Dump() const; static void LogErrorInventory(const std::string& log); diff --git a/src/modules/inventory/src/inventoryImp.cpp b/src/modules/inventory/src/inventoryImp.cpp index 950441f218..711804c2f7 100644 --- a/src/modules/inventory/src/inventoryImp.cpp +++ b/src/modules/inventory/src/inventoryImp.cpp @@ -352,6 +352,8 @@ void Inventory::UpdateChanges(const std::string& table, NotifyChange(result, data, table); } }; + + std::unique_lock lock{m_mutex}; DBSyncTxn txn { m_spDBSync->handle(), @@ -373,12 +375,16 @@ void Inventory::TryCatchTask(const std::function& task) const { if (!m_stopping) { - task(); // Ejecuta la tarea + task(); + } + else + { + LogTrace("No Scanning during stopping"); } } catch (const std::exception& ex) { - LogErrorInventory(std::string{ex.what()}); + LogError("{}",std::string{ex.what()}); } } @@ -422,15 +428,18 @@ void Inventory::Init(const std::shared_ptr& spInfo, m_spInfo = spInfo; m_reportDiffFunction = reportDiffFunction; - std::unique_lock lock{m_mutex}; - m_stopping = false; - m_spDBSync = std::make_unique(HostType::AGENT, - DbEngineType::SQLITE3, - dbPath, - GetCreateStatement(), - DbManagement::PERSISTENT); - m_spNormalizer = std::make_unique(normalizerConfigPath, normalizerType); - SyncLoop(lock); + { + std::unique_lock lock{m_mutex}; + m_stopping = false; + m_spDBSync = std::make_unique(HostType::AGENT, + DbEngineType::SQLITE3, + dbPath, + GetCreateStatement(), + DbManagement::PERSISTENT); + m_spNormalizer = std::make_unique(normalizerConfigPath, normalizerType); + } + + SyncLoop(); } void Inventory::Destroy() @@ -438,7 +447,6 @@ void Inventory::Destroy() std::unique_lock lock{m_mutex}; m_stopping = true; m_cv.notify_all(); - lock.unlock(); } @@ -671,7 +679,6 @@ nlohmann::json Inventory::GetNetworkData() networkTableData["network_item_id"] = GetItemId(networkTableData, NETWORK_ITEM_ID_FIELDS); ret[NETWORKS_TABLE].push_back(networkTableData); - } } @@ -733,6 +740,8 @@ void Inventory::ScanPackages() NotifyChange(result, data, PACKAGES_TABLE); } }; + + std::unique_lock lock{m_mutex}; DBSyncTxn txn { m_spDBSync->handle(), @@ -862,6 +871,7 @@ void Inventory::ScanProcesses() NotifyChange(result, data, PROCESSES_TABLE); } }; + std::unique_lock lock{m_mutex}; DBSyncTxn txn { m_spDBSync->handle(), @@ -902,19 +912,24 @@ void Inventory::Scan() LogInfo("Evaluation finished."); } -void Inventory::SyncLoop(std::unique_lock& lock) +void Inventory::SyncLoop() { - if (m_scanOnStart) + LogInfo("Module started."); + + if (m_scanOnStart && !m_stopping) { Scan(); } - while (!m_cv.wait_for(lock, std::chrono::milliseconds{m_intervalValue}, [&]() -{ - return m_stopping; -})) + while (!m_stopping) { + { + std::unique_lock lock{m_mutex}; + m_cv.wait_for(lock, + std::chrono::milliseconds{m_intervalValue}, [&]() { return m_stopping; } ); + } Scan(); } + std::unique_lock lock{m_mutex}; m_spDBSync.reset(nullptr); }