diff --git a/src/slic3r/plugin/PluginFsUtils.cpp b/src/slic3r/plugin/PluginFsUtils.cpp index 36cb1e8eb8..071a8331e0 100644 --- a/src/slic3r/plugin/PluginFsUtils.cpp +++ b/src/slic3r/plugin/PluginFsUtils.cpp @@ -140,6 +140,75 @@ bool delete_plugin_root(const boost::filesystem::path& resolved_root, const std: { namespace fs = boost::filesystem; +#ifdef _WIN32 + // Windows can report a deletion that is already under way as a transient error: a just-removed + // child can stay visible for a moment (ERROR_DIR_NOT_EMPTY, 145), a scanner or shell preview can + // still hold a handle (ERROR_SHARING_VIOLATION, 32), and Boost 1.84's child-open path maps + // STATUS_SHARING_VIOLATION to ERROR_NOT_SUPPORTED (50). Retry only those three codes, and keep + // each reported as returned: 50 and 145 are different failures and are never relabeled. + constexpr unsigned max_attempts = 5; + constexpr std::chrono::milliseconds retry_delays[max_attempts - 1] = { + std::chrono::milliseconds(100), std::chrono::milliseconds(250), + std::chrono::milliseconds(500), std::chrono::milliseconds(1000)}; + + const auto is_transient_delete_error = [](const boost::system::error_code& err) { + return err.category() == boost::system::system_category() && + (err.value() == ERROR_DIR_NOT_EMPTY || err.value() == ERROR_SHARING_VIOLATION || + err.value() == ERROR_NOT_SUPPORTED); + }; + + boost::system::error_code ec; + boost::system::error_code last_retry_error; + std::uintmax_t removed_count = 0; + unsigned attempts = 0; + + for (;;) { + ++attempts; + ec.clear(); + removed_count = fs::remove_all(resolved_root, ec); + if (!ec) + break; + + if (attempts < max_attempts && is_transient_delete_error(ec)) { + const std::chrono::milliseconds delay = retry_delays[attempts - 1]; + BOOST_LOG_TRIVIAL(warning) << "Failed to delete plugin folder " << resolved_root.string() + << " transiently: attempt=" << attempts << "/" << max_attempts + << " error_value=" << ec.value() << " error_category=" << ec.category().name() + << " message=" << ec.message() << " delay_ms=" << delay.count(); + last_retry_error = ec; + ::Sleep(static_cast(delay.count())); + continue; + } + + break; + } + + if (ec) { + // TRN %1% is a folder path, %2% the system error message. + error = GUI::format(_u8L("Failed to delete plugin folder %1%: %2%"), resolved_root.string(), ec.message()); + BOOST_LOG_TRIVIAL(error) << "Failed to delete plugin folder " << resolved_root.string() + << " after attempts=" << attempts << " error_value=" << ec.value() + << " error_category=" << ec.category().name() << " message=" << ec.message(); + return false; + } + + if (attempts > 1) { + // Recovered after one or more transient failures: keep the last failure for diagnosis. + BOOST_LOG_TRIVIAL(info) << "Deleted plugin: " << plugin_id << " from " << resolved_root.string() + << " after attempts=" << attempts << " last_error_value=" << last_retry_error.value() + << " last_error_category=" << last_retry_error.category().name() + << " last_error_message=" << last_retry_error.message(); + return true; + } + + if (removed_count == 0) { + return true; + } + + BOOST_LOG_TRIVIAL(info) << "Deleted plugin: " << plugin_id << " from " << resolved_root.string(); + return true; +#else + // Non-Windows behavior is unchanged: one remove_all() call, no retries. boost::system::error_code ec; const auto removed_count = fs::remove_all(resolved_root, ec); if (ec) { @@ -154,6 +223,7 @@ bool delete_plugin_root(const boost::filesystem::path& resolved_root, const std: BOOST_LOG_TRIVIAL(info) << "Deleted plugin: " << plugin_id << " from " << resolved_root.string(); return true; +#endif } diff --git a/src/slic3r/plugin/PluginManager.cpp b/src/slic3r/plugin/PluginManager.cpp index fb0166c214..7425f2d255 100644 --- a/src/slic3r/plugin/PluginManager.cpp +++ b/src/slic3r/plugin/PluginManager.cpp @@ -137,25 +137,6 @@ bool PluginManager::initialize() // Uninstalled in shutdown() before the interpreter finalizes. plugin_hooks::install(); - // Persist auto-load / capability state to each plugin's .install_state.json sidecar. - // On load: write enabled=true plus the current capability flags. On unload: flip enabled=false. - // The on-unload callback is skipped during shutdown, so app exit does not wipe the auto-load list. - subscribe_on_load_callback([this](const std::string& key) { write_loaded_plugin_install_state(key); }); - subscribe_on_unload_callback([this](const std::string& key) { - if (m_shutting_down.load(std::memory_order_acquire)) - return; - std::lock_guard state_lock(install_state_mutex()); - PluginDescriptor descriptor; - if (!try_get_plugin_descriptor(key, descriptor) || descriptor.plugin_root.empty()) - return; - const boost::filesystem::path root(descriptor.plugin_root); - PluginInstallState state; - if (read_install_state(root, state)) { - state.enabled = false; - write_install_state(root, state); - } - }); - BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": Plugin manager initialized"; return true; @@ -1075,6 +1056,11 @@ void PluginManager::load_plugin_impl(const std::string& plugin_key, bool skip_de clear_plugin_error(plugin_key); + // Persist only after the plugin is committed as loaded. Lifecycle callbacks are dispatched + // asynchronously from worker threads and must not own sidecar persistence. + if (!m_shutting_down.load(std::memory_order_acquire)) + write_loaded_plugin_install_state(plugin_key); + { std::lock_guard lock(m_mutex); m_load_errors.erase(plugin_key); @@ -1128,8 +1114,10 @@ bool PluginManager::unload_plugin(const std::string& plugin_key) // Teardown runs outside m_mutex: it calls into Python and must not hold the registry lock. plugin_loader::unload(removed); - // The .install_state.json sidecar is flipped to enabled=false by the on-unload callback - // (skipped during shutdown), so the auto-load list survives app exit. + // Persist before returning: callers may delete or replace the plugin directory next. + if (!m_shutting_down.load(std::memory_order_acquire)) + mark_plugin_install_state_disabled(plugin_key); + BOOST_LOG_TRIVIAL(info) << "Unloaded plugin: " << plugin_key; return true;