Why do I execute an external program and wait for about 30 seconds before executing it?

fn main() {
  println!("003: {:?}", timestamp());
  execute_shell_command(&compile_file_path,
                        &[],
                        &cur_dir);
  println!("005: {:?}", timestamp());
  println!("006: open {}", cur_dir);
}

fn execute_shell_command(command: &PathBuf, args: &[&String], cur_dir: &String) {
    unsafe {
        if VERIFY == false {
            return;
        }
    }

    let mut list_dir = Command::new(command);
    for arg in args {
        list_dir.arg(arg);
    }
    list_dir.current_dir(cur_dir);
    let output = list_dir.output().expect("failed to execute process");

    // println!("output {:?}", output);
    list_dir.status().expect("process failed to execute");

    println!("list_dir.status()");
}

timestamp returns the current timestamp.

The output of this program is as follows:

003: 1613202857
004 [] 1613202891
_loadLibrary
time: 1613202857 now_time: 1613202891
list_dir.status()
005: 1613202891
006: open /var/folders/5c/_sgp7ks16vv_3mldz8m0dpsm0000gn/T/.tmpqfcu8X

Process finished with exit code 0

Line 004 is printed by an external program, and the time stamp display is at least 34 seconds slower. Why is that?

The source code of the external program, the first line is output 004.

Strange phenomenon:

But when I set a breakpoint, it doesn't seem to be slow.

I obviously remember that the first time I did this, there was no such problem.

System: macOS Catalina

When I tested the source code separately, this problem did not occur

A question to ask here is how long the external program takes to run, because you appear to be running it twice: once without transmitting any output and once with output sent to standard output. Line 004 thus won't appear until after the first run of the program.

Also, as an aside, the unsafe block looks suspicious as it suggests VERIFY is a static mut variable, which is very unsafe, and not necessary since you could just use an AtomicBool in a static variable without unsafe. You should try to avoid global variables where possible though.

The reason is known, because it was executed twice. I will look into AtomicBool.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.