databendlabs/databend · error

Failed to create spawn function

Error message

Failed to create spawn function: {}

What it means

`setup_lua_environment` builds the `metactl` Lua namespace for metactl scripts. The async `spawn` closure is converted into a Lua function via mlua's `create_async_function`; if mlua cannot construct the function (e.g. a memory-allocation or callback-registration failure inside the Lua runtime), the error is wrapped in this anyhow message and aborts environment setup.

Solutions

  1. Check the inner `{}` error in the message for the mlua cause; if it is an out-of-memory error, reduce script/heap usage or raise the Lua memory limit.
  2. Verify the mlua crate build includes the `async` feature, since `create_async_function` is required for `metactl.spawn`.
  3. Re-run the operation in a fresh Lua instance — the existing state may be corrupted by an earlier script error or unsound userdata.
  4. Update/align mlua versions across the workspace if a version mismatch caused callback registration to fail.
Defensive patterns

Strategy: try-catch

Try / catch

match setup_lua_environment(&lua) {
    Err(e) if e.to_string().contains("Failed to create spawn function") => {
        // recreate the Lua instance and retry setup once
    }
    Err(e) => return Err(e),
    Ok(()) => {}
}

Prevention

When it happens

Trigger: Calling `run_lua_script`/`run_lua_script_with_result` (or the unit tests) when mlua's `Lua::create_async_function` fails while registering the `metactl.spawn` async function — practically only on Lua runtime allocation/callback-slot failures or a corrupted Lua state.

Common situations: Running metactl with embedded Lua scripting when the mlua runtime is degraded (out-of-memory in the Lua allocator, an unsafe/foreign caller that already poisoned the Lua state, or an mlua build without the required async feature flags).

Related errors


AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11). Data as JSON: /api/errors/383edf30ff35a7d5. Report an issue: GitHub.

Appendix: source

Thrown at src/meta/control/src/lua_support.rs:445

    // Register spawn function that delegates to tokio::task::spawn_local
    let spawn_fn = lua
        .create_function(|_lua, func: mlua::Function| {
            #[allow(clippy::disallowed_methods)]
            let handle = tokio::task::spawn_local(async move {
                match func.call_async::<mlua::Value>(()).await {
                    Ok(result) => result,
                    Err(e) => {
                        eprintln!("Spawned task error: {}", e);
                        mlua::Value::Nil
                    }
                }
            });

            Ok(LuaTask {
                handle: Rc::new(RefCell::new(Some(handle))),
            })
        })
        .map_err(|e| anyhow::anyhow!("Failed to create spawn function: {}", e))?;

    metactl_table
        .set("spawn", spawn_fn)
        .map_err(|e| anyhow::anyhow!("Failed to register spawn function: {}", e))?;

    // Register async sleep function
    let sleep_fn = lua
        .create_async_function(|_lua, seconds: f64| async move {
            let duration = duration_from_seconds(seconds, "sleep")?;
            time::sleep(duration).await;
            Ok(())
        })
        .map_err(|e| anyhow::anyhow!("Failed to create sleep function: {}", e))?;

    metactl_table
        .set("sleep", sleep_fn)
        .map_err(|e| anyhow::anyhow!("Failed to register sleep function: {}", e))?;

View on GitHub (pinned to 288d84d76e)