{"record":{"id":"383edf30ff35a7d5","repo":"databendlabs/databend","slug":"failed-to-create-spawn-function","errorCode":null,"errorMessage":"Failed to create spawn function: {}","messagePattern":"Failed to create spawn function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":445,"sourceCode":"    // Register spawn function that delegates to tokio::task::spawn_local\n    let spawn_fn = lua\n        .create_function(|_lua, func: mlua::Function| {\n            #[allow(clippy::disallowed_methods)]\n            let handle = tokio::task::spawn_local(async move {\n                match func.call_async::<mlua::Value>(()).await {\n                    Ok(result) => result,\n                    Err(e) => {\n                        eprintln!(\"Spawned task error: {}\", e);\n                        mlua::Value::Nil\n                    }\n                }\n            });\n\n            Ok(LuaTask {\n                handle: Rc::new(RefCell::new(Some(handle))),\n            })\n        })\n        .map_err(|e| anyhow::anyhow!(\"Failed to create spawn function: {}\", e))?;\n\n    metactl_table\n        .set(\"spawn\", spawn_fn)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register spawn function: {}\", e))?;\n\n    // Register async sleep function\n    let sleep_fn = lua\n        .create_async_function(|_lua, seconds: f64| async move {\n            let duration = duration_from_seconds(seconds, \"sleep\")?;\n            time::sleep(duration).await;\n            Ok(())\n        })\n        .map_err(|e| anyhow::anyhow!(\"Failed to create sleep function: {}\", e))?;\n\n    metactl_table\n        .set(\"sleep\", sleep_fn)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register sleep function: {}\", e))?;\n","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L427-L463","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["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.","Verify the mlua crate build includes the `async` feature, since `create_async_function` is required for `metactl.spawn`.","Re-run the operation in a fresh Lua instance — the existing state may be corrupted by an earlier script error or unsound userdata.","Update/align mlua versions across the workspace if a version mismatch caused callback registration to fail."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match setup_lua_environment(&lua) {\n    Err(e) if e.to_string().contains(\"Failed to create spawn function\") => {\n        // recreate the Lua instance and retry setup once\n    }\n    Err(e) => return Err(e),\n    Ok(()) => {}\n}","preventionTips":["Keep the mlua `async` feature enabled in Cargo.toml.","Never reuse a Lua instance after a fatal script error; build a fresh one per run.","Watch the inner mlua error text, not just the anyhow wrapper.","Pin a single mlua version across the workspace to avoid feature/ABI mismatches."],"tags":["lua","mlua","runtime-setup","rust"],"backgroundTag":"module-init-failed","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}