{"record":{"id":"5f5fc57edc077b95","repo":"DioxusLabs/dioxus","slug":"lazy-value-is-not-initialized-make-sure-to-call","errorCode":null,"errorMessage":"Lazy value is not initialized. Make sure to call `initialize` before dereferencing.","messagePattern":"Lazy value is not initialized\\. Make sure to call `initialize` before dereferencing\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/fullstack/src/lazy.rs","lineNumber":98,"sourceCode":"            if self\n                .started_initialization\n                .swap(true, std::sync::atomic::Ordering::SeqCst)\n            {\n                self.value.wait();\n                return Ok(());\n            }\n\n            // Otherwise, we need to initialize the value\n            self.set(constructor().unwrap())?;\n        }\n        Ok(())\n    }\n\n    /// Get a reference to the value of the `Lazy` instance. This will block the current thread if the\n    /// value is not yet initialized.\n    pub fn get(&self) -> &T {\n        if self.constructor.is_none() {\n            return self.value.get().expect(\"Lazy value is not initialized. Make sure to call `initialize` before dereferencing.\");\n        };\n\n        if self.value.get().is_none() {\n            self.initialize().expect(\"Failed to initialize lazy value\");\n        }\n\n        self.value.get().unwrap()\n    }\n}\n\nimpl<T: Send + Sync + 'static> Default for Lazy<T> {\n    fn default() -> Self {\n        Self::lazy()\n    }\n}\n\nimpl<T: Send + Sync + 'static> std::ops::Deref for Lazy<T> {\n    type Target = T;","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/fullstack/src/lazy.rs#L80-L116","documentation":"Lazy is a OnceLock-based global used by dioxus-fullstack for server values. A Lazy created with Lazy::lazy()/Default has no constructor, so get()/Deref requires that set()/try_set() was called first; when the constructor is None and the value was never set, this expect panics. Lazy values with constructors self-initialize on first access, but bare lazy() values do not.","triggerScenarios":"Dereferencing a Lazy created via Lazy::lazy() that was never initialized: accessing a fullstack server context or a Lazy global (e.g. a connection pool) outside of the dioxus::serve setup that should call set(), or forgetting the initialization step in the server config callback.","commonSituations":"Unit tests calling server helpers that deref a Lazy without booting the server; a binary path that never runs launch setup; refactoring that moves set() behind a disabled feature or config branch.","solutions":["Initialize the Lazy during server setup: lazy.set(value) or try_set(value) before any access","Create it with Lazy::new(constructor) instead of Lazy::lazy() so it self-initializes on first get()","Only deref the Lazy inside a running dioxus::serve app where setup guarantees initialization","In tests, call set()/initialize() in the harness before touching code that dereferences it"],"exampleFix":"// before\nstatic DB: Lazy<Db> = Lazy::lazy();\nfn handler() { DB.query(...); } // panics: never set\n// after\nstatic DB: Lazy<Db> = Lazy::new(|| async { connect().await });\n// or: DB.set(connect_now()) during dioxus::serve setup","handlingStrategy":"validation","validationCode":"// Expose an accessor instead of raw deref:\npub fn db() -> Option<&'static Db> {\n    DB.try_get()\n}\n// or ensure setup ran:\nfn ensure_initialized(lazy: &Lazy<Db>) -> Result<(), CapturedError> { lazy.initialize() }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer Lazy::new(constructor) over Lazy::lazy() so values self-initialize","Centralize set()/try_set() in one server setup function called by dioxus::serve","In tests, always run the serve setup (or call initialize()) before touching Lazy globals","Wrap Lazy access in an Option-returning helper instead of bare deref"],"tags":["fullstack","lazy","global-state","server","initialization","panic"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}