DioxusLabs/dioxus · error · CapturedError

Lazy value is already initialized.

Error message

Lazy value is already initialized.

What it means

Guard in fullstack Lazy::set: fires when set is called on a Lazy whose OnceLock already holds a value or whose initialization has already started. The docs specify set is meant to run once during server setup (typically inside dioxus::serve); a second call reaches this error with the provided value dropped.

Source

Thrown at packages/fullstack/src/lazy.rs:66

        // Prevent the constructor from being optimized out
        black_box(constructor);

        Self {
            _phantom: std::marker::PhantomData,
            value: std::sync::OnceLock::new(),
            started_initialization: AtomicBool::new(false),
            constructor: Some(blocking_initialize::<T, F, G, E>),
        }
    }

    /// Set the value of the `Lazy` instance.
    ///
    /// This should only be called once during the server setup phase, typically inside `dioxus::serve`.
    /// Future calls to this method will return an error containing the provided value.
    pub fn set(&self, pool: T) -> Result<(), CapturedError> {
        let res = self.value.set(pool);
        if res.is_err() {
            return Err(anyhow::anyhow!("Lazy value is already initialized.").into());
        }

        Ok(())
    }

    pub fn try_set(&self, pool: T) -> Result<(), T> {
        self.value.set(pool)
    }

    /// Initialize the value of the `Lazy` instance if it hasn't been initialized yet.
    pub fn initialize(&self) -> Result<(), CapturedError> {
        if let Some(constructor) = self.constructor {
            // If we're already initializing this value, wait on the receiver.
            if self
                .started_initialization
                .swap(true, std::sync::atomic::Ordering::SeqCst)
            {
                self.value.wait();

View on GitHub (pinned to 24f6a829df)

Solutions

  1. The lazy value was initialized twice. Initialize it only once.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/fullstack/src/lazy.rs:66 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23). Data as JSON: /api/errors/63c04f238d1454fc. Report an issue: GitHub.