From 5cdf5bf8fa798f8472267d3dfb7d913e9a371b96 Mon Sep 17 00:00:00 2001 From: Ian Bassi Date: Wed, 18 Nov 2026 12:28:45 -0300 Subject: [PATCH] Keep the preset sync thread away from the app while it shuts down Closing Orca while the startup sync of presets and plugins was running crashed on exit. The threads only saw their cancel flag once ~PresetUpdater ran, after the main window with its plater and the app config were gone, and the sync thread checked it in a few places only, so it went on to call into them. Read the app settings it needs before starting the thread, make the flag atomic and check it at each step, and stop and join the threads when the main window closes, and again in GUI_App::OnExit(). Ported from bambulab/BambuStudio@fc560922da862e59dedbfe6ea8ffcf284d3abb1f Co-authored-by: baijie --- src/slic3r/GUI/GUI_App.cpp | 3 +++ src/slic3r/GUI/MainFrame.cpp | 4 ++++ src/slic3r/Utils/PresetUpdater.cpp | 35 +++++++++++++++++++++++------- src/slic3r/Utils/PresetUpdater.hpp | 3 +++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index c5a7797fad..4a162b0bb6 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -2937,6 +2937,9 @@ int GUI_App::OnExit() destroy_automation(); stop_http_server(); stop_sync_user_preset(); + // Also stopped when the main window closes. Its threads read the app config, which ~GUI_App deletes before the updater. + if (preset_updater != nullptr) + preset_updater->stop(); if (m_startup_cloud_plugin_fetch_thread.joinable()) m_startup_cloud_plugin_fetch_thread.join(); diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 7431e3f1d3..cde7fdb5dd 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -116,6 +116,7 @@ #include "BindDialog.hpp" #include "../Utils/MacDarkMode.hpp" #include "../Utils/NetworkAgentFactory.hpp" +#include "../Utils/PresetUpdater.hpp" #include "../Utils/PrintHost.hpp" #include "GUI_App.hpp" @@ -755,6 +756,9 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, BORDERLESS_FRAME_ // Prevent queued selection/UI refresh work from running as normal during reset. wxGetApp().set_closing(true); wxGetApp().start_exit_watchdog(); + // The preset sync threads use the plater, which is deleted with this window. + if (PresetUpdater* updater = wxGetApp().get_preset_updater()) + updater->stop(); m_plater->reset(); this->shutdown(); diff --git a/src/slic3r/Utils/PresetUpdater.cpp b/src/slic3r/Utils/PresetUpdater.cpp index a8146eccda..f41f972ac1 100644 --- a/src/slic3r/Utils/PresetUpdater.cpp +++ b/src/slic3r/Utils/PresetUpdater.cpp @@ -243,7 +243,7 @@ struct PresetUpdater::priv fs::path rsrc_path; fs::path vendor_path; - bool cancel; + std::atomic cancel; std::thread thread; bool has_waiting_updates { false }; @@ -284,7 +284,7 @@ struct PresetUpdater::priv void check_new_vendors(const std::set& system_vendors, std::function, bool)> callback); void sync_tooltip(std::string http_url, std::string language); - void sync_plugins(std::string http_url, std::string plugin_version); + void sync_plugins(std::string http_url, std::string plugin_version, bool is_arm64, bool use_legacy_plugin); void sync_printer_config(std::string http_url); bool get_cached_plugins_version(std::string &cached_version, bool& force); @@ -864,13 +864,15 @@ bool PresetUpdater::priv::get_cached_plugins_version(std::string& cached_version return has_plugins; } -void PresetUpdater::priv::sync_plugins(std::string http_url, std::string plugin_version) +void PresetUpdater::priv::sync_plugins(std::string http_url, std::string plugin_version, bool is_arm64, bool use_legacy_plugin) { + if (cancel) return; + if (plugin_version == "00.00.00.00") { BOOST_LOG_TRIVIAL(info) << "non need to sync plugins for there is no plugins currently."; return; } - std::string curr_version = GUI::wxGetApp().use_legacy_network_plugin() ? BAMBU_NETWORK_AGENT_VERSION_LEGACY : get_latest_network_version(); + std::string curr_version = use_legacy_plugin ? BAMBU_NETWORK_AGENT_VERSION_LEGACY : get_latest_network_version(); std::string using_version = curr_version.substr(0, 9) + "00"; auto cache_plugin_folder = cache_path / "plugins"; @@ -931,7 +933,7 @@ void PresetUpdater::priv::sync_plugins(std::string http_url, std::string plugin_ } #if defined(__WINDOWS__) - if (GUI::wxGetApp().is_running_on_arm64() && !GUI::wxGetApp().use_legacy_network_plugin()) { + if (is_arm64 && !use_legacy_plugin) { //set to arm64 for plugins std::map current_headers = Slic3r::Http::get_extra_headers(); current_headers["X-BBL-OS-Type"] = "windows_arm"; @@ -951,7 +953,7 @@ void PresetUpdater::priv::sync_plugins(std::string http_url, std::string plugin_ BOOST_LOG_TRIVIAL(warning) << format("[Orca Updater] sync_plugins: %1%", e.what()); } #if defined(__WINDOWS__) - if (GUI::wxGetApp().is_running_on_arm64() && !GUI::wxGetApp().use_legacy_network_plugin()) { + if (is_arm64 && !use_legacy_plugin) { //set back std::map current_headers = Slic3r::Http::get_extra_headers(); current_headers["X-BBL-OS-Type"] = "windows"; @@ -961,6 +963,9 @@ void PresetUpdater::priv::sync_plugins(std::string http_url, std::string plugin_ } #endif + // the app may be shutting down, so don't touch the GUI + if (cancel) return; + bool result = get_cached_plugins_version(cached_version, force_upgrade); if (result) { BOOST_LOG_TRIVIAL(info) << format("[Orca Updater] found new plugins: %1%, prompt to update, force_upgrade %2%", cached_version, force_upgrade); @@ -1039,6 +1044,7 @@ void PresetUpdater::priv::sync_printer_config(std::string http_url) result = true; } } catch (...) {} + if (cancel) return; if (result) { BOOST_LOG_TRIVIAL(info) << format("[Orca Updater] found new printer config: %1%, prompt to update", cached_version); waiting_printer_updates = get_printer_config_updates(true); @@ -1357,6 +1363,11 @@ PresetUpdater::PresetUpdater() : // Public PresetUpdater::~PresetUpdater() +{ + this->stop(); +} + +void PresetUpdater::stop() { if (p && p->thread.joinable()) { // This will stop transfers being done by the thread, if any. @@ -1379,15 +1390,23 @@ void PresetUpdater::sync(std::string http_url, std::string language, std::string //p->set_download_prefs(GUI::wxGetApp().app_config); if (!p->enabled_version_check && !p->enabled_config_update) { return; } - p->thread = std::thread([this, http_url, language, plugin_version]() { + // read on the main thread, wxGetApp() is null during shutdown + bool is_arm64 = false; +#if defined(__WINDOWS__) + is_arm64 = GUI::wxGetApp().is_running_on_arm64(); +#endif + const bool use_legacy_plugin = GUI::wxGetApp().use_legacy_network_plugin(); + p->thread = std::thread([this, http_url, language, plugin_version, is_arm64, use_legacy_plugin]() { try { this->p->sync_version(); if (p->cancel) return; // Vendor profile updates are triggered by check_vendor_update() // after the startup printer preset has been restored. - this->p->sync_plugins(http_url, plugin_version); + this->p->sync_plugins(http_url, plugin_version, is_arm64, use_legacy_plugin); this->p->sync_printer_config(http_url); + if (p->cancel) + return; // Orca (PR #130): the filament library is always installed, so refresh it // from the updater on every startup sync rather than deferring to check_vendor_update(). this->p->sync_vendor_config(PresetBundle::ORCA_FILAMENT_LIBRARY); diff --git a/src/slic3r/Utils/PresetUpdater.hpp b/src/slic3r/Utils/PresetUpdater.hpp index 22571e52d9..abbf13336a 100644 --- a/src/slic3r/Utils/PresetUpdater.hpp +++ b/src/slic3r/Utils/PresetUpdater.hpp @@ -28,6 +28,9 @@ public: PresetUpdater &operator=(const PresetUpdater &) = delete; ~PresetUpdater(); + // Stops the background threads and waits for them. To be called while the app they read from still exists. + void stop(); + // If either version check or config updating is enabled, get the appropriate data in the background and cache it. void sync(std::string http_url, std::string language, std::string plugin_version, PresetBundle *preset_bundle);