Skip to content

builtin/custom command may become blocking when pipe is full #22

@rust-shell-script

Description

@rust-shell-script

Right now for builtin/custom commands, we just call the registered functions to get the results instead of spawning separate processes. However, when they are used in pipes, it could become blocking when the pipe is full:

code:

rust_cmd_lib git:(master) ✗ cat examples/one.rs 
use cmd_lib::*;

fn main() -> CmdResult {
    init_builtin_logger();
    use_builtin_cmd!(cat);    // remove this line, there will be no blocking
    let big_file_content = "a".repeat(64 * 4096 * 2);
    std::fs::write("/tmp/big_file", big_file_content)?;
    run_cmd!(cat /tmp/big_file | wc)?;
    Ok(())
}

output:

rust_cmd_lib git:(master) ✗ CMD_LIB_DEBUG=1 cargo run --example one
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/examples/one`
DEBUG - Running ["cat", "/tmp/big_file"] | ["wc"] ...
---> blocking here

We can fix the above bug by launching threads for each builtin/custom commands, however, it seems overkill for other builtin commands like echo/true/info/error/debug/..., and it would also complicate the current code. Need to figure out a way to solve this blocking issue more gracefully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions