{"record":{"id":"638938ca94c8f524","repo":"databendlabs/databend","slug":"failed-to-create-now-ms-function","errorCode":null,"errorMessage":"Failed to create now_ms function: {}","messagePattern":"Failed to create now_ms function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":470,"sourceCode":"    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)\n        .eval::<Table>()\n        .map_err(|e| anyhow::anyhow!(\"Failed to load zipf_load_generator: {}\", e))?;\n\n    metactl_table\n        .set(\"ZipfGenerator\", zipf_generator)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register ZipfGenerator: {}\", e))?;\n\n    // Set the metactl table as a global","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L452-L488","documentation":"A monotonic `metactl.now_ms` clock is registered with mlua's synchronous `create_function`, capturing the setup-time `Instant`. If mlua cannot create the Lua callback wrapper for this closure, the error is wrapped as `Failed to create now_ms function` and environment setup fails.","triggerScenarios":"`lua.create_function(move |_lua, ()| ...)` for `now_ms` failing during `setup_lua_environment` — practically only on Lua allocator/callback-registration failures in the shared runtime.","commonSituations":"Running metactl Lua scripts in a runtime with exhausted memory or a corrupted Lua state; rarely, an mlua build issue where `create_function` callback slots cannot be allocated.","solutions":["Check the inner mlua error text; treat memory errors by recreating the Lua instance or raising MemoryLimits.","Confirm the closure's captured types (`Instant`) are Send/Sync-compatible with the selected mlua features if the build errors instead.","Re-run in a fresh Lua environment; do not reuse a state that already errored fatally.","Align/upgrade mlua versions workspace-wide if the inner error indicates an mlua regression."],"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 now_ms function\") => {\n        // treat as environment setup failure; rebuild Lua state\n    }\n    other => other?,\n}","preventionTips":["Use a fresh Lua instance per script run so callback slots are never exhausted.","Keep captured state (e.g. Instant) simple and compatible with the mlua feature set.","Keep mlua on one pinned version workspace-wide.","Log the underlying mlua error 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"}