{"record":{"id":"aa8f05c9f29cde59","repo":"databendlabs/databend","slug":"failed-to-create-sleep-function","errorCode":null,"errorMessage":"Failed to create sleep function: {}","messagePattern":"Failed to create sleep function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":458,"sourceCode":"\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();\n    let now_ms_fn = lua\n        .create_function(move |_lua, ()| Ok(start.elapsed().as_secs_f64() * 1000.0))\n        .map_err(|e| anyhow::anyhow!(\"Failed to create now_ms function: {}\", e))?;\n\n    metactl_table\n        .set(\"now_ms\", now_ms_fn)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register now_ms function: {}\", e))?;\n\n    // Register the Zipf load generator. The module returns the `ZipfGenerator`","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L440-L476","documentation":"`setup_lua_environment` registers `metactl.sleep` as an async function that sleeps for a given number of seconds. If mlua's `create_async_function` cannot build the callback for this closure, the failure is wrapped in this message and Lua environment setup fails.","triggerScenarios":"`lua.create_async_function(...)` for the sleep closure failing during environment setup inside `run_lua_script`, `run_lua_script_with_result`, or the unit tests — essentially only on mlua runtime/allocation failures.","commonSituations":"Out-of-memory in the embedded Lua allocator, a corrupted Lua state reused after a previous script panicked, or an mlua build lacking async support.","solutions":["Read the inner mlua error; if it is a memory error, raise the Lua memory limit or recreate the Lua instance.","Ensure the mlua dependency is built with the `async` feature so `create_async_function` is supported.","Do not reuse a Lua instance that previously hit a fatal error; create a new one per script run.","Pin/upgrade mlua if the inner error suggests an mlua bug, and report with a reproducer."],"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 sleep function\") => {\n        // recreate Lua state or propagate a clear setup failure\n    }\n    other => other?,\n}","preventionTips":["Ensure the mlua build has async support for create_async_function.","Recreate the Lua instance after any fatal runtime error instead of continuing.","Keep mlua versions aligned across workspace crates.","Surface the inner mlua error, not just the wrapper message."],"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"}