databendlabs/databend · error

Failed to register now_ms function

Error message

Failed to register now_ms function: {}

What it means

After creation, the `now_ms` closure is stored into the `metactl` table with `Table::set`. If mlua fails to push or store that function value, the error is surfaced as `Failed to register now_ms function` and setup aborts before scripts execute.

Solutions

  1. Inspect the inner mlua error; recreate the Lua instance for memory/stack errors.
  2. Ensure `now_ms_fn` and `metactl_table` come from the same `Lua` instance.
  3. Raise or remove custom Lua memory limits if the inner error is a memory error.
  4. If it reproduces on a clean state, collect the inner error and report/upgrade mlua.
Defensive patterns

Strategy: try-catch

Try / catch

if let Err(e) = setup_lua_environment(&lua) {
    if e.to_string().contains("Failed to register now_ms function") {
        // recreate the Lua environment before retrying the script
    }
}

Prevention

When it happens

Trigger: `metactl_table.set("now_ms", now_ms_fn)` returning an mlua::Error during `setup_lua_environment`, i.e. during any Lua-script run or its unit tests.

Common situations: Lua stack/memory exhaustion in the shared Lua instance, typically caused by an earlier script that exhausted resources or corrupted the state.

Related errors


AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11). Data as JSON: /api/errors/2f4764886dea2951. Report an issue: GitHub.

Appendix: source

Thrown at src/meta/control/src/lua_support.rs:474

            Ok(())
        })
        .map_err(|e| anyhow::anyhow!("Failed to create sleep function: {}", e))?;

    metactl_table
        .set("sleep", sleep_fn)
        .map_err(|e| anyhow::anyhow!("Failed to register sleep function: {}", e))?;

    // Register a monotonic millisecond clock for timing and benchmarks. The
    // epoch is this environment's setup time; only differences between calls are
    // meaningful. `Instant` guarantees the value never goes backwards.
    let start = std::time::Instant::now();
    let now_ms_fn = lua
        .create_function(move |_lua, ()| Ok(start.elapsed().as_secs_f64() * 1000.0))
        .map_err(|e| anyhow::anyhow!("Failed to create now_ms function: {}", e))?;

    metactl_table
        .set("now_ms", now_ms_fn)
        .map_err(|e| anyhow::anyhow!("Failed to register now_ms function: {}", e))?;

    // Register the Zipf load generator. The module returns the `ZipfGenerator`
    // table, exposed as `metactl.ZipfGenerator` so any script can call
    // `metactl.ZipfGenerator:new(num_keys, alpha)` directly.
    let zipf_generator = lua
        .load(ZIPF_LOAD_GENERATOR)
        .eval::<Table>()
        .map_err(|e| anyhow::anyhow!("Failed to load zipf_load_generator: {}", e))?;

    metactl_table
        .set("ZipfGenerator", zipf_generator)
        .map_err(|e| anyhow::anyhow!("Failed to register ZipfGenerator: {}", e))?;

    // Set the metactl table as a global
    lua.globals()
        .set("metactl", metactl_table)
        .map_err(|e| anyhow::anyhow!("Failed to register metactl namespace: {}", e))?;

View on GitHub (pinned to 288d84d76e)