From a6cad8a6e56bca5ac7355468f16cb26567a5c26c Mon Sep 17 00:00:00 2001 From: peachismomo Date: Wed, 30 Sep 2026 19:27:50 +0800 Subject: [PATCH] fix: printer agent and camera shutdown races --- src/slic3r/GUI/Monitor.cpp | 5 ++++- src/slic3r/Utils/MoonrakerPrinterAgent.cpp | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/slic3r/GUI/Monitor.cpp b/src/slic3r/GUI/Monitor.cpp index eb5a8eb5e9..91bbbf8930 100644 --- a/src/slic3r/GUI/Monitor.cpp +++ b/src/slic3r/GUI/Monitor.cpp @@ -276,7 +276,10 @@ void MonitorPanel::select_machine(std::string machine_sn) 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(); //Layout(); } diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp index 63d6c99604..c13a5f20d9 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include namespace { @@ -357,6 +358,7 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params } ConnectionSettings connection; uint64_t gen; + std::thread previous_connect_thread; { std::lock_guard lock(connect_mutex); init_device_info(params); @@ -367,11 +369,15 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params connection.use_ssl = device_info.use_ssl; connection.ca_file = device_info.ca_file; 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(); { std::lock_guard lock(cmd_mutex); @@ -399,15 +405,21 @@ int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params int MoonrakerPrinterAgent::disconnect_printer() { + std::thread previous_connect_thread; { std::lock_guard lock(connect_mutex); device_info = MoonrakerDeviceInfo{}; ++connect_generation; // Invalidate any in-flight connection 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(); { std::lock_guard lock(cmd_mutex);