From ace2bc4ab68f4d5b55791654c111a022014c2608 Mon Sep 17 00:00:00 2001 From: Gianmatteo Palmieri Date: Fri, 19 Jul 2024 15:56:36 +0200 Subject: [PATCH] fix(libsinsp): disable thread pool on webassembly Signed-off-by: Gianmatteo Palmieri --- userspace/libsinsp/plugin.cpp | 10 ++++++++++ userspace/libsinsp/sinsp.cpp | 2 ++ userspace/libsinsp/test/plugins.ut.cpp | 4 ++++ userspace/libsinsp/test/thread_pool.ut.cpp | 4 +++- userspace/plugin/plugin_api.h | 2 +- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/userspace/libsinsp/plugin.cpp b/userspace/libsinsp/plugin.cpp index 4b91a4a2ecc..ec98143c62e 100755 --- a/userspace/libsinsp/plugin.cpp +++ b/userspace/libsinsp/plugin.cpp @@ -824,6 +824,11 @@ std::vector sinsp_plugin::get_metrics() const thread_pool::routine_id_t sinsp_plugin::subscribe_routine(ss_plugin_routine_fn_t routine_fn, ss_plugin_routine_state_t* routine_state) { + if(!m_thread_pool) + { + return static_cast(nullptr); + } + auto f = [this, routine_fn, routine_state]() -> bool { return static_cast(routine_fn(m_state, routine_state)); }; @@ -833,6 +838,11 @@ thread_pool::routine_id_t sinsp_plugin::subscribe_routine(ss_plugin_routine_fn_t void sinsp_plugin::unsubscribe_routine(thread_pool::routine_id_t routine_id) { + if(!m_thread_pool || !routine_id) + { + return; + } + m_thread_pool->unsubscribe(routine_id); } diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 3eb745941d4..2bc5490e481 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -132,7 +132,9 @@ sinsp::sinsp(bool static_container, const std::string &static_id, const std::str m_table_registry = std::make_shared(); m_table_registry->add_table(m_thread_manager.get()); +#if !defined(__EMSCRIPTEN__) m_thread_pool = std::make_shared(); +#endif } sinsp::~sinsp() diff --git a/userspace/libsinsp/test/plugins.ut.cpp b/userspace/libsinsp/test/plugins.ut.cpp index d3a413c30f4..8215e2d5bc9 100644 --- a/userspace/libsinsp/test/plugins.ut.cpp +++ b/userspace/libsinsp/test/plugins.ut.cpp @@ -921,6 +921,8 @@ TEST_F(sinsp_with_test_input, plugin_metrics) #endif +#if !defined(__EMSCRIPTEN__) + TEST_F(sinsp_with_test_input, plugin_routines) { auto p = register_plugin(&m_inspector, get_plugin_api_sample_routines); @@ -957,3 +959,5 @@ TEST_F(sinsp_with_test_input, plugin_routines) routines_num = m_inspector.m_thread_pool->routines_num(); ASSERT_EQ(routines_num, 0); } + +#endif diff --git a/userspace/libsinsp/test/thread_pool.ut.cpp b/userspace/libsinsp/test/thread_pool.ut.cpp index ec8265c487c..b4124110fcf 100644 --- a/userspace/libsinsp/test/thread_pool.ut.cpp +++ b/userspace/libsinsp/test/thread_pool.ut.cpp @@ -19,6 +19,7 @@ limitations under the License. #include #include +#if !defined(__EMSCRIPTEN__) TEST_F(sinsp_with_test_input, thread_pool) { open_inspector(); @@ -69,4 +70,5 @@ TEST_F(sinsp_with_test_input, thread_pool) ASSERT_EQ(tp->routines_num(), 1); m_inspector.close(); ASSERT_EQ(tp->routines_num(), 0); -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/userspace/plugin/plugin_api.h b/userspace/plugin/plugin_api.h index b0b6a476fbe..14a373e78be 100644 --- a/userspace/plugin/plugin_api.h +++ b/userspace/plugin/plugin_api.h @@ -416,7 +416,7 @@ typedef struct // - f: the function executed by the routine on each iteration // - i: the routine's state // - // Return value: A routine handle that can be used to later unsubscribe the routine. + // Return value: A routine handle that can be used to later unsubscribe the routine. Returns null in case of failure. ss_plugin_routine_t* (*subscribe)(ss_plugin_owner_t* o, ss_plugin_routine_fn_t f, ss_plugin_routine_state_t* i); //