Bound Plugin Disconnect and Python Shutdown When OrcaSlicer Exits (#16765)

* Bound Plugin Disconnect and Python Shutdown When OrcaSlicer Exits

A Python printer agent whose disconnect_printer() never returned froze
the closing app, and a plugin thread that never ended kept the process
alive after its window closed, because Py_Finalize joins every
non-daemon thread.

At shutdown a Python agent's disconnect_printer() now runs once per
agent on a helper thread with a 3 second deadline, holding a reference
that keeps the plugin object alive if the call outlives a forced
unload. PythonInterpreter::shutdown() waits up to 3 seconds for running
plugin calls and for non-daemon threads; if either is still going, the
interpreter is left unfinalized for the process exit to end.

* Run the Threading Atexit Hooks Before Waiting for Plugin Threads

An idle concurrent.futures worker only ends when the hooks registered with
threading._register_atexit() tell it to, and Py_Finalize runs those after
the shutdown wait, so a plugin that left an executor open made every exit
wait the full deadline and skip finalization. The hooks now run first, on a
daemon thread joined within the same deadline, since they join their
workers without a timeout.
This commit is contained in:
HanifKoh authored and GitHub committed 2026-10-26 03:52:09 +08:00
1 parent 88890b9bb1
commit 75252a70f7
6 files changed
+310 -7

No files matched your search

@@ -1098,6 +1098,61 @@ TEST_CASE("The bundled Python trusts the system CA certificates", "[PluginLifecy
CHECK(default_context_ca_count() > 0);
}
TEST_CASE("Waiting for Python threads at shutdown gives up on a thread that does not end", "[PluginLifecycle][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
pybind11::gil_scoped_acquire gil;
const pybind11::module_ threading = pybind11::module_::import("threading");
const pybind11::object release = threading.attr("Event")();
pybind11::object worker = threading.attr("Thread")(pybind11::arg("target") = release.attr("wait"), pybind11::arg("daemon") = false);
worker.attr("start")();
const auto start = std::chrono::steady_clock::now();
CHECK(PythonInterpreter::join_non_daemon_threads(std::chrono::milliseconds(200)) == 1);
CHECK(std::chrono::steady_clock::now() - start < std::chrono::seconds(5));
release.attr("set")();
CHECK(PythonInterpreter::join_non_daemon_threads(std::chrono::seconds(5)) == 0);
}
TEST_CASE("Waiting for Python threads at shutdown ends idle concurrent.futures workers", "[PluginLifecycle][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
pybind11::gil_scoped_acquire gil;
const pybind11::module_ threading = pybind11::module_::import("threading");
const pybind11::module_ futures = pybind11::module_::import("concurrent.futures");
const pybind11::module_ workers = pybind11::module_::import("concurrent.futures.thread");
// The hooks that end the workers also refuse new tasks for the rest of the process; the
// interpreter outlives this test case.
const auto allow_new_tasks = [&workers] { workers.attr("_shutdown") = false; };
// An idle worker waits for its queue until the hooks tell it to end; the wait must not run out.
pybind11::object executor = futures.attr("ThreadPoolExecutor")(pybind11::arg("max_workers") = 1);
executor.attr("submit")(pybind11::module_::import("builtins").attr("int")).attr("result")();
auto start = std::chrono::steady_clock::now();
CHECK(PythonInterpreter::join_non_daemon_threads(std::chrono::seconds(5)) == 0);
CHECK(std::chrono::steady_clock::now() - start < std::chrono::seconds(2));
allow_new_tasks();
// A worker still in a task blocks the hooks; both are given up at the deadline.
const pybind11::object release = threading.attr("Event")();
executor = futures.attr("ThreadPoolExecutor")(pybind11::arg("max_workers") = 1);
executor.attr("submit")(release.attr("wait"));
start = std::chrono::steady_clock::now();
CHECK(PythonInterpreter::join_non_daemon_threads(std::chrono::milliseconds(200)) == 1);
CHECK(std::chrono::steady_clock::now() - start < std::chrono::seconds(5));
release.attr("set")();
CHECK(PythonInterpreter::join_non_daemon_threads(std::chrono::seconds(5)) == 0);
allow_new_tasks();
}
#ifndef _WIN32 // Windows adds its system store whatever SSL_CERT_FILE names.
TEST_CASE("SSL_CERT_FILE still overrides the bundled Python's default CA certificates", "[PluginLifecycle][Python]")
{