{"record":{"id":"8f28d766fae576b4","repo":"linebender/druid","slug":"application-is-already-running","errorCode":null,"errorMessage":"Application is already running","messagePattern":"Application is already running","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"druid-shell/src/application.rs","lineNumber":131,"sourceCode":"        util::assert_main_thread_or_main_unclaimed();\n        GLOBAL_APP.with(|global_app| global_app.borrow().clone())\n    }\n\n    /// Start the `Application` runloop.\n    ///\n    /// The provided `handler` will be used to inform of events.\n    ///\n    /// This will consume the `Application` and block the current thread\n    /// until the `Application` has finished executing.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the `Application` is already running.\n    pub fn run(self, handler: Option<Box<dyn AppHandler>>) {\n        // Make sure this application hasn't run() yet.\n        if let Ok(mut state) = self.state.try_borrow_mut() {\n            if state.running {\n                panic!(\"Application is already running\");\n            }\n            state.running = true;\n        } else {\n            panic!(\"Application state already borrowed\");\n        }\n\n        // Run the platform application\n        self.backend_app.run(handler);\n\n        // This application is no longer active, so clear the global reference\n        GLOBAL_APP.with(|global_app| {\n            *global_app.borrow_mut() = None;\n        });\n        // .. and release the main thread\n        util::release_main_thread();\n        // .. and mark as done so a new sequence can start\n        APPLICATION_CREATED\n            .compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire)","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/linebender/druid/blob/0f8b1195e4e073f9597f2865299c3d18f8e4005f/druid-shell/src/application.rs#L113-L149","documentation":"`Application::run` panics if called on an `Application` instance that has already been run. The application tracks a `running` flag in a `RefCell`; calling `run` twice on the same instance would start a second platform event loop, which is invalid.","triggerScenarios":"Calling `app.run(handler)` a second time on the same `Application` value (or a clone/reborrow of it) after the first `run` set `state.running = true`.","commonSituations":"Restarting the app from a menu/callback by calling `run` again instead of creating a new `Application`; calling run in both a setup path and a main path.","solutions":["Create a fresh `Application` for each run instead of reusing the previous instance","Restructure the code so `run` is called exactly once, from one code path","If a restart is needed, spawn a new process rather than re-running the same `Application`"],"exampleFix":"// before\nlet app = Application::new().unwrap();\napp.run(None);\napp.run(None); // panics\n\n// after\nlet app = Application::new().unwrap();\napp.run(None); // once; make a new Application to run again","handlingStrategy":"type-guard","validationCode":"// Track run state in the caller:\nlet mut has_run = false;\nif !has_run { app.run(None); has_run = true; }","typeGuard":"fn can_run(running: bool) -> bool { !running }","tryCatchPattern":"// Rust panics are not catchable here by design; guard the call site:\nif !app_has_run() { app.run(handler); }","preventionTips":["Call `Application::run` exactly once from the program entry point","Never store and re-invoke the same `Application` for restarts","Route restarts through spawning a new process or a fresh `Application`"],"tags":["panic","invalid-state","application-lifecycle","druid-shell"],"backgroundTag":"invalid-state-transition","analyzedSha":"0f8b1195e4e073f9597f2865299c3d18f8e4005f","analyzedAt":"2026-09-10T14:27:34.582Z","contentChangedAt":"2026-09-10T14:27:34.582Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}