Skip to content

Commit

Permalink
Merge pull request #384 from wazuh/fix/358-agent-hangs-during-a-conne…
Browse files Browse the repository at this point in the history
…ction-attempt-and-delays-shutdown

Agent delays shutdown while Scanning
  • Loading branch information
cborla authored Dec 3, 2024
2 parents 554b973 + 7f1c7c9 commit 527d17d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/modules/inventory/include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Inventory {
void ScanPorts();
void ScanProcesses();
void Scan();
void SyncLoop(std::unique_lock<std::mutex>& lock);
void SyncLoop();
void ShowConfig();
cJSON * Dump() const;
static void LogErrorInventory(const std::string& log);
Expand Down
53 changes: 34 additions & 19 deletions src/modules/inventory/src/inventoryImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ void Inventory::UpdateChanges(const std::string& table,
NotifyChange(result, data, table);
}
};

std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand All @@ -373,12 +375,16 @@ void Inventory::TryCatchTask(const std::function<void()>& 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()});
}
}

Expand Down Expand Up @@ -422,23 +428,25 @@ void Inventory::Init(const std::shared_ptr<ISysInfo>& spInfo,
m_spInfo = spInfo;
m_reportDiffFunction = reportDiffFunction;

std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = false;
m_spDBSync = std::make_unique<DBSync>(HostType::AGENT,
DbEngineType::SQLITE3,
dbPath,
GetCreateStatement(),
DbManagement::PERSISTENT);
m_spNormalizer = std::make_unique<InvNormalizer>(normalizerConfigPath, normalizerType);
SyncLoop(lock);
{
std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = false;
m_spDBSync = std::make_unique<DBSync>(HostType::AGENT,
DbEngineType::SQLITE3,
dbPath,
GetCreateStatement(),
DbManagement::PERSISTENT);
m_spNormalizer = std::make_unique<InvNormalizer>(normalizerConfigPath, normalizerType);
}

SyncLoop();
}

void Inventory::Destroy()
{
std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = true;
m_cv.notify_all();
lock.unlock();
}


Expand Down Expand Up @@ -671,7 +679,6 @@ nlohmann::json Inventory::GetNetworkData()
networkTableData["network_item_id"] = GetItemId(networkTableData, NETWORK_ITEM_ID_FIELDS);

ret[NETWORKS_TABLE].push_back(networkTableData);

}
}

Expand Down Expand Up @@ -733,6 +740,8 @@ void Inventory::ScanPackages()
NotifyChange(result, data, PACKAGES_TABLE);
}
};

std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand Down Expand Up @@ -862,6 +871,7 @@ void Inventory::ScanProcesses()
NotifyChange(result, data, PROCESSES_TABLE);
}
};
std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand Down Expand Up @@ -902,19 +912,24 @@ void Inventory::Scan()
LogInfo("Evaluation finished.");
}

void Inventory::SyncLoop(std::unique_lock<std::mutex>& 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<std::mutex> lock{m_mutex};
m_cv.wait_for(lock,
std::chrono::milliseconds{m_intervalValue}, [&]() { return m_stopping; } );
}
Scan();
}
std::unique_lock<std::mutex> lock{m_mutex};
m_spDBSync.reset(nullptr);
}

0 comments on commit 527d17d

Please sign in to comment.