{"record":{"id":"51535e8b83e39e8e","repo":"databendlabs/databend","slug":"failed-to-register-spawn-function","errorCode":null,"errorMessage":"Failed to register spawn function: {}","messagePattern":"Failed to register spawn function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":449,"sourceCode":"            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\n    // Register a monotonic millisecond clock for timing and benchmarks. The\n    // epoch is this environment's setup time; only differences between calls are\n    // meaningful. `Instant` guarantees the value never goes backwards.\n    let start = std::time::Instant::now();","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L431-L467","documentation":"After the `spawn` function is created it is stored in the `metactl` table with `Table::set`. mlua's `set` serializes the key/value into Lua; if that fails (Lua stack exhaustion, memory error, or a value that cannot be pushed), the error is wrapped as `Failed to register spawn function` and setup aborts.","triggerScenarios":"`metactl_table.set(\"spawn\", spawn_fn)` returning an mlua::Error during `setup_lua_environment`, i.e. during any `run_lua_script`/`run_lua_script_with_result` call or the associated tests.","commonSituations":"Lua state stack/memory exhaustion from a prior runaway script, or running in an mlua build where function values cannot be stored (sandboxed/limited runtime).","solutions":["Inspect the inner mlua error; memory/stack errors usually mean the Lua state should be recreated rather than reused.","Confirm `metactl_table` was created in the same Lua instance as `spawn_fn`; cross-instance values fail to push.","Raise or remove any custom Lua memory limit (MemoryLimits) if the inner error is MemoryError.","Retry in a fresh environment; if reproducible, file against mlua with the inner error text."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = setup_lua_environment(&lua) {\n    if e.to_string().contains(\"Failed to register spawn function\") {\n        // rebuild Lua state / surface inner mlua error to the operator\n    }\n}","preventionTips":["Avoid setting custom MemoryLimits tighter than the default in the Lua runtime.","Create all values (tables, functions) from the same Lua instance.","Treat any earlier script panic in this Lua state as fatal and rebuild it.","Log the inner mlua error chain for diagnosis."],"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"}