databendlabs/databend · error

Failed to register sleep function

Error message

Failed to register sleep function: {}

What it means

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.

Solutions

  1. Examine the inner mlua error; stack/memory errors indicate the Lua state should be recreated.
  2. Verify `sleep_fn` and `metactl_table` belong to the same `Lua` instance.
  3. Increase or remove custom MemoryLimits configured on the Lua runtime.
  4. If reproducible in a clean state, capture 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 sleep function") {
        // rebuild the Lua environment and retry once
    }
}

Prevention

When it happens

Trigger: `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).

Common situations: Lua stack overflow or memory exhaustion inside the shared Lua instance, often triggered by an earlier runaway script in the same runtime.

Related errors


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

Appendix: source

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

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

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

    // Register async sleep function
    let sleep_fn = lua
        .create_async_function(|_lua, seconds: f64| async move {
            let duration = duration_from_seconds(seconds, "sleep")?;
            time::sleep(duration).await;
            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)

View on GitHub (pinned to 288d84d76e)