{"record":{"id":"e931b1217b663e86","repo":"tauri-apps/tauri","slug":"state-called-before-manage-for","errorCode":null,"errorMessage":"state() called before manage() for {}","messagePattern":"state\\(\\) called before manage\\(\\) for (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tauri/src/lib.rs","lineNumber":734,"sourceCode":"  where\n    T: Send + Sync + 'static,\n  {\n    // The caller decides to break the safety here, then OK, just let it go.\n    unsafe { self.manager().state.unmanage() }\n  }\n\n  /// Retrieves the managed state for the type `T`.\n  ///\n  /// # Panics\n  ///\n  /// Panics if the state for the type `T` has not been previously [managed](Self::manage).\n  /// Use [try_state](Self::try_state) for a non-panicking version.\n  fn state<T>(&self) -> State<'_, T>\n  where\n    T: Send + Sync + 'static,\n  {\n    self.manager().state.try_get().unwrap_or_else(|| {\n      panic!(\n        \"state() called before manage() for {}\",\n        std::any::type_name::<T>()\n      )\n    })\n  }\n\n  /// Attempts to retrieve the managed state for the type `T`.\n  ///\n  /// Returns `Some` if the state has previously been [managed](Self::manage). Otherwise returns `None`.\n  fn try_state<T>(&self) -> Option<State<'_, T>>\n  where\n    T: Send + Sync + 'static,\n  {\n    self.manager().state.try_get()\n  }\n\n  /// Get a reference to the resources table of this manager.\n  fn resources_table(&self) -> MutexGuard<'_, ResourceTable>;","sourceCodeStart":716,"sourceCodeEnd":752,"githubUrl":"https://github.com/tauri-apps/tauri/blob/460ec35447493200d64290dd7f015d5a91d0fd58/crates/tauri/src/lib.rs#L716-L752","documentation":"The `state<T>()` accessor on Manager/App/AppHandle/Window retrieves state previously registered with `manage::<T>()`. Tauri panics with the type name when no such state exists, because accessing unmanaged state is a programming error; `try_state` is the non-panicking alternative.","triggerScenarios":"Calling `app.state::<T>()` (or handle/window.state) before `app.manage(T)` was called, or for a type that was never managed (different concrete type, e.g. managing `Config` but requesting `MyConfig`, or managing `Arc<Config>` vs `Config` mismatch).","commonSituations":"Accessing state from a command or event handler that runs before setup completes; a refactor renaming the state type; double-managed confusion where a wrapper type was stored instead of the inner type.","solutions":["Call `app.manage(MyType::new(...))` in `Builder::setup` before any code accesses `state::<MyType>()`.","Use `app.try_state::<T>()` and handle the None case gracefully instead of panicking.","Ensure the exact type matches: `state::<Arc<Config>>()` vs `state::<Config>()` must match what was passed to manage().","If state is produced asynchronously, block/gate access (e.g. Option in a Mutex managed upfront) rather than deferring manage()."],"exampleFix":"// before\n#[tauri::command]\nfn get_count(app: AppHandle) -> i32 { app.state::<Counter>().0 } // panics if never managed\n// after\n#[tauri::command]\nfn get_count(app: AppHandle) -> i32 {\n  app.try_state::<Counter>().map(|c| c.0).unwrap_or(0)\n}\n// and in setup:\n.setup(|app| { app.manage(Counter(0)); Ok(()) })","handlingStrategy":"fallback","validationCode":"if app.try_state::<Counter>().is_none() {\n  app.manage(Counter(0));\n}\nlet counter = app.state::<Counter>(); // safe now","typeGuard":null,"tryCatchPattern":"// if using try_state in a command\nlet counter = app.try_state::<Counter>()\n  .ok_or_else(|| tauri::Error::Anyhow(anyhow::anyhow!(\"state not initialized\")))?;","preventionTips":["Manage all state inside Builder::setup before run()","Always request the exact type passed to manage() (watch Arc<T> vs T)","Prefer try_state().ok_or(...) in commands to return errors, not panic","Never defer manage() behind async work that commands may race with"],"tags":["rust","tauri","state","panic"],"backgroundTag":"resource-not-found","analyzedSha":"460ec35447493200d64290dd7f015d5a91d0fd58","analyzedAt":"2026-09-18T23:55:51.277Z","contentChangedAt":"2026-09-18T23:55:51.277Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}