databendlabs/databend · error

Failed to create sleep function

Error message

Failed to create sleep function: {}

What it means

`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.

Solutions

  1. Read the inner mlua error; if it is a memory error, raise the Lua memory limit or recreate the Lua instance.
  2. Ensure the mlua dependency is built with the `async` feature so `create_async_function` is supported.
  3. Do not reuse a Lua instance that previously hit a fatal error; create a new one per script run.
  4. Pin/upgrade mlua if the inner error suggests an mlua bug, and report with a reproducer.
Defensive patterns

Strategy: try-catch

Try / catch

match setup_lua_environment(&lua) {
    Err(e) if e.to_string().contains("Failed to create sleep function") => {
        // recreate Lua state or propagate a clear setup failure
    }
    other => other?,
}

Prevention

When it happens

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

Common situations: 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.

Related errors


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

Appendix: source

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

            Ok(LuaTask {
                handle: Rc::new(RefCell::new(Some(handle))),
            })
        })
        .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`

View on GitHub (pinned to 288d84d76e)