Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Inventory::SyncLoop to Avoid Blocking Agent Stop #445

Open
cborla opened this issue Dec 18, 2024 · 1 comment
Open

Improve Inventory::SyncLoop to Avoid Blocking Agent Stop #445

cborla opened this issue Dec 18, 2024 · 1 comment
Labels
level/task Task issue module/agent module/inventory Inventory module type/enhancement Enhancement issue

Comments

@cborla
Copy link
Member

cborla commented Dec 18, 2024

Description

The current implementation of the Inventory::SyncLoop function in the Inventory module may cause unnecessary delays during the agent shutdown process. Specifically, the Scan() method is called after the stop condition (m_stopping) is triggered, leading to potential delays if Scan() is a time-consuming operation.

Problem Details

  • The Scan() function is executed after the condition variable (m_cv.wait_for) returns, even if m_stopping is true.
  • This behavior delays the shutdown of the agent when Scan() involves intensive operations or long-running tasks.
  • This could lead to poor user experience during shutdown and inefficient resource handling.

Steps to Reproduce

  • Start the agent with the Inventory module enabled.
  • Trigger a stop request for the agent while SyncLoop is running.
  • Observe that Scan() executes even after the stop request, delaying the shutdown.

Proposed Solution

Update the SyncLoop function to verify the m_stopping condition after the wait operation and before calling Scan(). This ensures that no unnecessary Scan() operation is performed after a stop request is issued.

Option

void Inventory::SyncLoop()
{
    LogInfo("Module started.");

    if (m_scanOnStart && !m_stopping)
    {
        Scan();
    }

    while (!m_stopping)
    {
        {
            std::unique_lock<std::mutex> lock{m_mutex};
            m_cv.wait_for(lock,
                std::chrono::milliseconds{m_intervalValue}, [&]() { return m_stopping; });
        }

        if (!m_stopping)
        {
            Scan();
        }
    }
    std::unique_lock<std::mutex> lock{m_mutex};
    m_spDBSync.reset(nullptr);
}
@cborla cborla added level/task Task issue module/agent module/inventory Inventory module type/enhancement Enhancement issue labels Dec 18, 2024
@wazuhci wazuhci moved this to Backlog in Release 5.0.0 Dec 18, 2024
@jr0me
Copy link
Member

jr0me commented Dec 19, 2024

As a note for when this issue goes into development, the first scan

    if (m_scanOnStart && !m_stopping)
    {
        Scan();
    }

Can happen while another threads calls Stop() on the Inventory, perhaps this scan should also try to capture the mutex before checking m_stopping, and block the Stop call until the scan finishes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
level/task Task issue module/agent module/inventory Inventory module type/enhancement Enhancement issue
Projects
Status: Backlog
Development

No branches or pull requests

2 participants