fix: printer agent and camera shutdown races

This commit is contained in:
peachismomo
2026-09-30 19:27:50 +08:00
parent 9bac75dda5
commit a6cad8a6e5
2 changed files with 19 additions and 4 deletions
+4 -1
View File
@@ -276,7 +276,10 @@ void MonitorPanel::select_machine(std::string machine_sn)
void MonitorPanel::on_timer(wxTimerEvent& event) void MonitorPanel::on_timer(wxTimerEvent& event)
{ {
if (update_flag) { // MediaPlayCtrl may yield the event loop while it joins its camera worker
// during window teardown. Do not let a queued monitor refresh touch panels
// that are already being destroyed.
if (!wxGetApp().is_closing() && update_flag) {
update_all(); update_all();
//Layout(); //Layout();
} }
+15 -3
View File
@@ -30,6 +30,7 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <thread> #include <thread>
#include <utility>
#include <variant> #include <variant>
namespace { namespace {
@@ -357,6 +358,7 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params
} }
ConnectionSettings connection; ConnectionSettings connection;
uint64_t gen; uint64_t gen;
std::thread previous_connect_thread;
{ {
std::lock_guard<std::recursive_mutex> lock(connect_mutex); std::lock_guard<std::recursive_mutex> lock(connect_mutex);
init_device_info(params); init_device_info(params);
@@ -367,11 +369,15 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params
connection.use_ssl = device_info.use_ssl; connection.use_ssl = device_info.use_ssl;
connection.ca_file = device_info.ca_file; connection.ca_file = device_info.ca_file;
if (connect_thread.joinable()) { if (connect_thread.joinable()) {
connect_thread.detach(); previous_connect_thread = std::move(connect_thread);
} }
} }
// Stop existing status stream and clear state // Join the previous connection worker before stopping the stream: it may be
// just about to start that stream after completing its HTTP setup.
if (previous_connect_thread.joinable()) {
previous_connect_thread.join();
}
stop_status_stream(); stop_status_stream();
{ {
std::lock_guard<std::mutex> lock(cmd_mutex); std::lock_guard<std::mutex> lock(cmd_mutex);
@@ -399,15 +405,21 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params
int MoonrakerPrinterAgent::disconnect_printer() int MoonrakerPrinterAgent::disconnect_printer()
{ {
std::thread previous_connect_thread;
{ {
std::lock_guard<std::recursive_mutex> lock(connect_mutex); std::lock_guard<std::recursive_mutex> lock(connect_mutex);
device_info = MoonrakerDeviceInfo{}; device_info = MoonrakerDeviceInfo{};
++connect_generation; // Invalidate any in-flight connection ++connect_generation; // Invalidate any in-flight connection
if (connect_thread.joinable()) { if (connect_thread.joinable()) {
connect_thread.detach(); previous_connect_thread = std::move(connect_thread);
} }
} }
// The connection worker may have started a stream immediately before it was
// invalidated, so join it before the final stream shutdown.
if (previous_connect_thread.joinable()) {
previous_connect_thread.join();
}
stop_status_stream(); stop_status_stream();
{ {
std::lock_guard<std::mutex> lock(cmd_mutex); std::lock_guard<std::mutex> lock(cmd_mutex);