diff --git a/tests/slic3rutils/python_test_support.hpp b/tests/slic3rutils/python_test_support.hpp index e9b0aaac8d..347812f0aa 100644 --- a/tests/slic3rutils/python_test_support.hpp +++ b/tests/slic3rutils/python_test_support.hpp @@ -2,6 +2,15 @@ // Shared embedded-interpreter bootstrap for slic3rutils tests that need a live Python // interpreter (test_plugin_host_api.cpp, test_slicing_pipeline_bindings.cpp, ...). +// +// The runtime is process-wide: once initialized it stays up for the rest of the test process. +// CPython cannot be reinitialized after Py_Finalize (a second init aborts inside _datetime), and +// the app likewise finalizes it only at exit, so nothing here tears it down. +// slic3rutils_tests_main.cpp finalizes the production interpreter once, after the last test case. +// +// This bootstrap is separate from PythonInterpreter's runtime: whichever of the two brings Python +// up first in a process owns it, and the other cannot initialize it again. ctest runs one case per +// process, so that only matters if the binary is run directly. #include #include #include diff --git a/tests/slic3rutils/slic3rutils_tests_main.cpp b/tests/slic3rutils/slic3rutils_tests_main.cpp index f8d2b4fac5..b23e6731cd 100644 --- a/tests/slic3rutils/slic3rutils_tests_main.cpp +++ b/tests/slic3rutils/slic3rutils_tests_main.cpp @@ -1,8 +1,40 @@ #include #include +#include +#include + +#include +#include + +#include "catch2/interfaces/catch_interfaces_reporter.hpp" #include "slic3r/Utils/Http.hpp" +namespace { + +// Finalizes the embedded interpreter once, after the last test case, and never between cases. +// CPython cannot be reinitialized after Py_Finalize (a second init aborts inside _datetime), and +// the app likewise finalizes it only at exit, so the per-test fixtures tear down only the +// PluginManager. Running at the end of the run, rather than from a static destructor, also keeps +// shutdown()'s logging ahead of boost::log's teardown. +class PythonInterpreterRunEnd final : public Catch::EventListenerBase +{ +public: + using Catch::EventListenerBase::EventListenerBase; + + void testRunEnded(const Catch::TestRunStats&) override + { + if (!Slic3r::PythonInterpreter::instance().is_initialized()) + return; + Slic3r::PluginManager::instance().shutdown(); + Slic3r::PythonInterpreter::instance().shutdown(); + } +}; + +} // namespace + +CATCH_REGISTER_LISTENER(PythonInterpreterRunEnd) + TEST_CASE("Check SSL certificates paths", "[Http][NotWorking]") { Slic3r::Http g = Slic3r::Http::get("https://github.com/"); @@ -58,4 +90,3 @@ TEST_CASE("Http basic authentication", "[Http][NotWorking]") { REQUIRE(status == 200); } - diff --git a/tests/slic3rutils/test_plugin_audit.cpp b/tests/slic3rutils/test_plugin_audit.cpp index aade7736e1..8b04e8faf5 100644 --- a/tests/slic3rutils/test_plugin_audit.cpp +++ b/tests/slic3rutils/test_plugin_audit.cpp @@ -557,7 +557,8 @@ class Handler(http.server.BaseHTTPRequestHandler): pass server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) -threading.Thread(target=server.serve_forever, daemon=True).start() +server_thread = threading.Thread(target=server.serve_forever, daemon=True) +server_thread.start() PORT = server.server_address[1] BASE = "http://127.0.0.1:%d" % PORT URL_A, URL_B, URL_REDIRECT = BASE + "/a", BASE + "/b", BASE + "/redirect" @@ -613,10 +614,14 @@ std::string describe(const std::vector& prompts) // The plugin system with the audit hook installed, and the permission dialog replaced by a // recorder that answers with `approve`. Declare it first in a test, so it is destroyed last. +// +// Only the manager is torn down here; the interpreter is finalized once per test process by +// PythonInterpreterRunEnd in slic3rutils_tests_main.cpp. struct AuditHarness { // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log. - // data_dir() is a global allowed root from then on, so the test files live in `outside`. + // data_dir() is a global allowed root from then on -- the first test to initialize Python in + // this process (under ctest, this one) -- so the test files live in `outside`. ScopedDataDir data{"plugin-audit-hook"}; bool initialized = PluginManager::instance().initialize(); ScopedTemporaryDir outside{"orca-plugin-audit-outside"}; @@ -625,6 +630,11 @@ struct AuditHarness AuditHarness() { + PluginAuditManager& audit = PluginAuditManager::instance(); + audit.clear_current_plugin(); + audit.clear_current_capability(); + audit.clear_audit_denial(); + audit.clear_last_violation(); PluginAuditManager::set_permission_prompt_for_testing( [this](const std::string& event, const std::vector& targets) { prompts.push_back({PluginAuditManager::instance().current_plugin(), event, targets}); @@ -636,7 +646,6 @@ struct AuditHarness { PluginAuditManager::set_permission_prompt_for_testing(nullptr); PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); } // Call before taking the GIL: discovery is not Python work. @@ -669,7 +678,7 @@ struct AuditHarness }; // The test-side Python globals. Construct and destroy it with the GIL held; it stops the HTTP -// server before the interpreter shuts down. +// server before the test case ends, so nothing keeps serving once the test's temp dirs are gone. struct PythonSide { py::dict globals; @@ -682,7 +691,7 @@ struct PythonSide } // A Python error fails the test here, as text: an error_already_set left to propagate would - // outlive the interpreter the harness shuts down. + // outlive the test case and keep a traceback with references into the interpreter. void exec(const char* code) { std::string error; @@ -698,8 +707,11 @@ struct PythonSide ~PythonSide() { try { + // Stop the server thread while Python is still up: the interpreter now outlives the + // test case, so a daemon thread must not keep serving (and touching test temp dirs). globals["server"].attr("shutdown")(); globals["server"].attr("server_close")(); + globals["server_thread"].attr("join")(); } catch (...) { } } diff --git a/tests/slic3rutils/test_plugin_capability_config.cpp b/tests/slic3rutils/test_plugin_capability_config.cpp index bbd0368d26..bd8a91e356 100644 --- a/tests/slic3rutils/test_plugin_capability_config.cpp +++ b/tests/slic3rutils/test_plugin_capability_config.cpp @@ -27,10 +27,10 @@ using json = nlohmann::json; namespace { -// Brings the plugin system up for the duration of one test and tears it down at the end — same -// idiom as ScopedPluginManager in test_plugin_lifecycle.cpp, and for the same reason: shutdown() -// logs via boost::log, so it must run before boost::log tears down its thread-local storage, not be -// left to a static destructor at process exit. +// Brings the plugin system up for the duration of one test and tears the manager down at the end — +// same idiom as ScopedPluginManager in test_plugin_lifecycle.cpp. The interpreter itself is +// process-wide and finalized once by PythonInterpreterRunEnd in slic3rutils_tests_main.cpp: +// CPython cannot be reinitialized after Py_Finalize. // // Needed here (unlike a bare pybind11::scoped_interpreter): has_config_ui()/get_config_ui()/ // get_name()/etc. all cross PyPluginCommonTrampoline's ORCA_PY_OVERRIDE_AUDITED, which refuses to @@ -40,7 +40,7 @@ namespace { // // initialize() leaves the GIL released (production code re-acquires it per call via PythonGILState); // every TEST_CASE below pairs this with a py::gil_scoped_acquire, declared second so it releases -// before this destructor's shutdown() runs. +// before this destructor runs. struct ScopedPluginManager { // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log, @@ -48,11 +48,7 @@ struct ScopedPluginManager ScopedDataDir python_data_dir{"plugin-python"}; bool initialized = PluginManager::instance().initialize(); - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; py::module_ import_orca_module() diff --git a/tests/slic3rutils/test_plugin_cloud_metadata.cpp b/tests/slic3rutils/test_plugin_cloud_metadata.cpp index 95ae6fcd43..3e3735a6ca 100644 --- a/tests/slic3rutils/test_plugin_cloud_metadata.cpp +++ b/tests/slic3rutils/test_plugin_cloud_metadata.cpp @@ -27,12 +27,11 @@ using json = nlohmann::json; namespace { -// Brings the plugin manager up and shuts both singletons down while boost::log is still alive; -// left to their static destructors, shutdown()'s logging runs after boost::log tears down its -// thread-local storage and crashes the process on exit (same reason ScopedPluginManager exists in -// test_plugin_lifecycle.cpp). initialize() IS needed here: the plugin under test is a -// slicing-pipeline script, so discover_plugins() brings up the Python interpreter to parse it, -// same as any other plugin. +// Brings the plugin manager up for the duration of one test and tears the manager down at the end. +// The interpreter itself is process-wide: PythonInterpreterRunEnd in slic3rutils_tests_main.cpp +// finalizes it once, after the last test case, because CPython cannot be reinitialized after +// Py_Finalize. initialize() IS needed here: the plugin under test is a slicing-pipeline script, so +// discover_plugins() brings up the Python interpreter to parse it, same as any other plugin. struct ScopedManagerShutdown { // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log, @@ -40,11 +39,7 @@ struct ScopedManagerShutdown ScopedDataDir python_data_dir{"plugin-python"}; bool initialized = PluginManager::instance().initialize(); - ~ScopedManagerShutdown() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedManagerShutdown() { PluginManager::instance().shutdown(); } }; const char* const CLOUD_PLUGIN_SOURCE = R"PY(# /// script diff --git a/tests/slic3rutils/test_plugin_json_depth.cpp b/tests/slic3rutils/test_plugin_json_depth.cpp index ad3c834b93..85f84d40e8 100644 --- a/tests/slic3rutils/test_plugin_json_depth.cpp +++ b/tests/slic3rutils/test_plugin_json_depth.cpp @@ -3,7 +3,6 @@ #include #include -#include #include "plugin_test_utils.hpp" @@ -19,17 +18,14 @@ using namespace Slic3r; namespace { -// Brings the embedded interpreter up for one test and tears it down before boost::log does, -// mirroring the ScopedPluginManager idiom in the other plugin tests. +// Brings the embedded interpreter up for one test and tears the manager down afterwards; the +// interpreter itself is process-wide and finalized once by PythonInterpreterRunEnd in +// slic3rutils_tests_main.cpp (CPython cannot be reinitialized after Py_Finalize). struct ScopedPluginManager { ScopedDataDir python_data_dir{"plugin-json-depth"}; bool initialized = PluginManager::instance().initialize(); - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; } // namespace diff --git a/tests/slic3rutils/test_plugin_lifecycle.cpp b/tests/slic3rutils/test_plugin_lifecycle.cpp index 0a1a94dcef..fc32511336 100644 --- a/tests/slic3rutils/test_plugin_lifecycle.cpp +++ b/tests/slic3rutils/test_plugin_lifecycle.cpp @@ -41,16 +41,19 @@ namespace fs = boost::filesystem; // -> unload. // // Each Catch2 test case runs in its own process (catch_discover_tests), so the PluginManager and -// interpreter singletons are brought up at most once per test. +// interpreter singletons are brought up at most once per test. Running the binary directly runs +// every case in one process: the interpreter is then initialized once and finalized after the +// last case (see PythonInterpreterRunEnd in slic3rutils_tests_main.cpp). namespace { -// Brings the plugin system up, and tears it down explicitly at the end of the test. +// Brings the plugin system up, and tears the manager down at the end of the test. // -// Shutting the interpreter down here, rather than leaving it to PythonInterpreter's static -// destructor, mirrors what the app does (GUI_App finalizes it before exit). Left to static -// destruction, shutdown()'s logging runs after boost::log has torn down its thread-local storage -// and throws, aborting the process after the tests have already passed. +// The interpreter itself is not finalized here: it is process-wide. CPython cannot be +// reinitialized after Py_Finalize (a second init aborts inside _datetime), and the app likewise +// finalizes it only at exit (GUI_App). slic3rutils_tests_main.cpp finalizes it once, after the +// last test case and before static destruction, where shutdown()'s logging is still ahead of +// boost::log's teardown. // // Declare this FIRST in a test so it is destroyed last. struct ScopedPluginManager @@ -61,11 +64,7 @@ struct ScopedPluginManager bool initialized = false; ScopedPluginManager() { initialized = PluginManager::instance().initialize(); } - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; // A minimal script plugin exposing exactly one capability, "Echo". diff --git a/tests/slic3rutils/test_plugin_printer_agent.cpp b/tests/slic3rutils/test_plugin_printer_agent.cpp index fc504c806a..7b5c2ca9f9 100644 --- a/tests/slic3rutils/test_plugin_printer_agent.cpp +++ b/tests/slic3rutils/test_plugin_printer_agent.cpp @@ -26,7 +26,9 @@ using namespace Slic3r; namespace { // Same idiom as ScopedPluginManager in test_plugin_lifecycle.cpp: the trampolines refuse to call -// into Python unless PythonInterpreter::instance() reports initialized. +// into Python unless PythonInterpreter::instance() reports initialized. Only the manager is torn +// down here; the interpreter is finalized once per test process by PythonInterpreterRunEnd in +// slic3rutils_tests_main.cpp. struct ScopedPluginManager { // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log, @@ -34,11 +36,7 @@ struct ScopedPluginManager ScopedDataDir python_data_dir{"plugin-python"}; bool initialized = PluginManager::instance().initialize(); - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; // The host reaches a printer agent plugin through IPrinterAgent, so the tests do too. The Python diff --git a/tests/slic3rutils/test_plugin_resource_texture.cpp b/tests/slic3rutils/test_plugin_resource_texture.cpp index c57a39b5e1..c4c9feab9e 100644 --- a/tests/slic3rutils/test_plugin_resource_texture.cpp +++ b/tests/slic3rutils/test_plugin_resource_texture.cpp @@ -69,8 +69,10 @@ namespace fs = boost::filesystem; namespace { -// Brings the plugin system up, and tears it down explicitly at the end of the test, as -// test_plugin_lifecycle.cpp does. Declare this FIRST in a test so it is destroyed last. +// Brings the plugin system up for the duration of one test and tears the manager down at the end, +// as test_plugin_lifecycle.cpp does. The interpreter is process-wide and finalized once by +// PythonInterpreterRunEnd in slic3rutils_tests_main.cpp; CPython cannot be reinitialized after +// Py_Finalize. Declare this FIRST in a test so it is destroyed last. struct ScopedPluginManager { // Before initialize(): the interpreter creates {data_dir}/python/packages and {data_dir}/log. @@ -78,11 +80,7 @@ struct ScopedPluginManager bool initialized = false; ScopedPluginManager() { initialized = PluginManager::instance().initialize(); } - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; // The app installs these in GUI_App::init_plugin_gui_wiring. Without an app the callbacks run inline diff --git a/tests/slic3rutils/test_slicing_pipeline_config.cpp b/tests/slic3rutils/test_slicing_pipeline_config.cpp index 0dc12639a0..73a3840577 100644 --- a/tests/slic3rutils/test_slicing_pipeline_config.cpp +++ b/tests/slic3rutils/test_slicing_pipeline_config.cpp @@ -53,11 +53,9 @@ struct ScopedPluginManager bool initialized = false; ScopedPluginManager() { initialized = PluginManager::instance().initialize(); } - ~ScopedPluginManager() - { - PluginManager::instance().shutdown(); - PythonInterpreter::instance().shutdown(); - } + // The interpreter is process-wide; PythonInterpreterRunEnd in slic3rutils_tests_main.cpp + // finalizes it once, after the last test case. + ~ScopedPluginManager() { PluginManager::instance().shutdown(); } }; const char* const CONFIG_PROBE_SOURCE = R"PY(# /// script