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

@@ -10,8 +10,10 @@
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <chrono>
#include <memory>
#include <string>
#include <thread>
#include <catch2/catch_test_macros.hpp>
#include "plugin_test_utils.hpp"
@@ -184,3 +186,68 @@ TEST_CASE("A working printer agent's answers reach the host unchanged", "[Plugin
CHECK(agent->bind_detect("192.168.0.2", "secure", detect) == 0);
CHECK(detect.dev_id == "192.168.0.2");
}
TEST_CASE("A printer agent's disconnect_printer is not waited for past its deadline while the app closes", "[PluginPrinterAgent][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
py::gil_scoped_acquire gil;
auto agent = make_agent(" release = __import__('threading').Event()\n"
" calls = 0\n"
" def disconnect_printer(self):\n"
" type(self).calls += 1\n"
" type(self).release.wait()\n"
" return 0\n");
REQUIRE(agent.agent);
const auto capability = std::dynamic_pointer_cast<PluginCapabilityInterface>(agent.agent);
REQUIRE(capability);
PluginManager::instance().set_shutting_down();
int first = 0;
int second = 0;
std::chrono::steady_clock::duration waited{};
{
// As at app exit: the closing UI thread does not hold the GIL. It disconnects the agent twice,
// once as the live agent and once from the agent cache.
py::gil_scoped_release nogil;
const auto start = std::chrono::steady_clock::now();
first = agent->disconnect_printer();
second = agent->disconnect_printer();
waited = std::chrono::steady_clock::now() - start;
}
CHECK(first == -1);
CHECK(second == -1);
CHECK(waited < std::chrono::seconds(5)); // one 3 s deadline, not two
// At most once: on a slow machine the call can be abandoned before its thread reaches Python.
CHECK(agent.instance.attr("calls").cast<int>() <= 1);
// Let the abandoned call finish before the agent is destroyed.
agent.instance.attr("release").attr("set")();
{
py::gil_scoped_release nogil;
for (int i = 0; i < 500 && capability->ref_count() > 0; ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
CHECK(capability->ref_count() == 0);
}
TEST_CASE("A printer agent's disconnect_printer still answers while the app closes", "[PluginPrinterAgent][Python]")
{
ScopedPluginManager plugin_system;
if (!plugin_system.initialized)
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
py::gil_scoped_acquire gil;
auto agent = make_agent(" def disconnect_printer(self): return 5\n");
REQUIRE(agent.agent);
PluginManager::instance().set_shutting_down();
int result = 0;
{
py::gil_scoped_release nogil;
result = agent->disconnect_printer();
}
CHECK(result == 5);
}