{"record":{"id":"1d31d3f93d837783","repo":"databendlabs/databend","slug":"failed-to-register-sleep-function","errorCode":null,"errorMessage":"Failed to register sleep function: {}","messagePattern":"Failed to register sleep function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":462,"sourceCode":"        })\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`\n    // table, exposed as `metactl.ZipfGenerator` so any script can call\n    // `metactl.ZipfGenerator:new(num_keys, alpha)` directly.\n    let zipf_generator = lua\n        .load(ZIPF_LOAD_GENERATOR)","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L444-L480","documentation":"Once the sleep closure is created it is assigned to `metactl.sleep` via `Table::set`. If pushing or storing the function value in the Lua table fails, the error is surfaced as `Failed to register sleep function` and setup aborts before any script runs.","triggerScenarios":"`metactl_table.set(\"sleep\", sleep_fn)` returning an mlua error during `setup_lua_environment` (called by `run_lua_script`, `run_lua_script_with_result`, and the Lua unit tests).","commonSituations":"Lua stack overflow or memory exhaustion inside the shared Lua instance, often triggered by an earlier runaway script in the same runtime.","solutions":["Examine the inner mlua error; stack/memory errors indicate the Lua state should be recreated.","Verify `sleep_fn` and `metactl_table` belong to the same `Lua` instance.","Increase or remove custom MemoryLimits configured on the Lua runtime.","If reproducible in a clean state, capture the inner error and report/upgrade mlua."],"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 sleep function\") {\n        // rebuild the Lua environment and retry once\n    }\n}","preventionTips":["Register all table members in one setup pass on a freshly created Lua instance.","Avoid custom memory limits that starve the runtime during registration.","Always create functions and the metactl table in the same Lua instance.","Preserve the full anyhow error chain when logging."],"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"}