Python
add safe mode to prevent plugins crashing startup
Chris Malone9a5a4edb35d7
diff --git a/shared/utils/plugins.py b/shared/utils/plugins.pyindex bd03304..fd45431 100644--- a/shared/utils/plugins.py+++ b/shared/utils/plugins.py@@ -1094,9 +1094,13 @@ class PluginManager:discovered.append(item)return sorted(discovered)- def load_plugins_from_directory(self, enabled_user_plugins: List[str]) -> None:+ def load_plugins_from_directory(self, enabled_user_plugins: List[str], safe_mode: bool = False) -> None:self.custom_js_snippets = []- plugins_to_load = SYSTEM_PLUGINS + [p for p in enabled_user_plugins if p not in SYSTEM_PLUGINS]+ if safe_mode:+ print("[Safe Mode] User plugins are disabled. Only system plugins will be loaded.")+ plugins_to_load = SYSTEM_PLUGINS+ else:+ plugins_to_load = SYSTEM_PLUGINS + [p for p in enabled_user_plugins if p not in SYSTEM_PLUGINS]for plugin_dir_name in self.discover_plugins():if plugin_dir_name not in plugins_to_load:@@ -1246,8 +1250,11 @@ class WAN2GPApplication:def initialize_plugins(self, wgp_globals: dict):if not hasattr(self, 'plugin_manager'):return-- auto_install_and_enable_default_plugins(self.plugin_manager, wgp_globals)++ safe_mode = wgp_globals.get("SAFE_MODE", False)++ if not safe_mode:+ auto_install_and_enable_default_plugins(self.plugin_manager, wgp_globals)server_config = wgp_globals.get("server_config")server_config_filename = wgp_globals.get("server_config_filename", "")@@ -1258,7 +1265,8 @@ class WAN2GPApplication:self.plugin_manager.cleanup_pending_deletions()self.enabled_plugins = server_config.get("enabled_plugins", [])- self.plugin_manager.load_plugins_from_directory(self.enabled_plugins)++ self.plugin_manager.load_plugins_from_directory(self.enabled_plugins, safe_mode=safe_mode)self.plugin_manager.inject_globals(wgp_globals)def setup_ui_tabs(self, main_tabs_component: gr.Tabs, state_component: gr.State, set_save_form_event):diff --git a/wgp.py b/wgp.pyindex d775d9f..99d213d 100644--- a/wgp.py+++ b/wgp.py@@ -11330,6 +11330,33 @@ if __name__ == "__main__":# Normal Gradio mode continues below...atexit.register(autosave_queue)++ STARTUP_LOCK_FILE = "startup.lock"+ globals()["SAFE_MODE"] = False++ if os.path.exists(STARTUP_LOCK_FILE):+ print("\n" + "!"*10)+ print("DETECTED FAILED PREVIOUS STARTUP. ENTERING SAFE MODE.")+ print("All user plugins are disabled to allow the server to start.")+ print("!"*10 + "\n")+ globals()["SAFE_MODE"] = True++ try:+ with open(STARTUP_LOCK_FILE, "w") as f:+ f.write(str(time.time()))+ except Exception as e:+ print(f"Warning: Could not create startup lock file: {e}")++ def mark_startup_success():+ time.sleep(30)+ if os.path.exists(STARTUP_LOCK_FILE):+ try:+ os.remove(STARTUP_LOCK_FILE)+ except:+ pass++ threading.Thread(target=mark_startup_success, daemon=True).start()+download_ffmpeg()# threading.Thread(target=runner, daemon=True).start()os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"