Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command::new("cargo-zigbuild") doesn't shutdown when aborting #194

Closed
cybersoulK opened this issue Oct 5, 2023 · 4 comments
Closed

Command::new("cargo-zigbuild") doesn't shutdown when aborting #194

cybersoulK opened this issue Oct 5, 2023 · 4 comments
Labels
good first issue Good for newcomers

Comments

@cybersoulK
Copy link

cybersoulK commented Oct 5, 2023

let mut process = tokio::process::Command::new("cargo-zigbuild")
                .args(["zigbuild", "--bin", &bin, "--target", &target, release])
                .spawn().unwrap();
                
                
 tokio::time::sleep(Duration::from_secs(4)).await;
let res = process.kill().await; 
println!("{res:?}");
               
process.wait().await.unwrap();

It prints Ok(()) to console, but continues running.

tried the following as well: (both std::process and tokio::process have the same issue)

Command::new("cargo-zigbuild").arg("build") 
and
 Command::new("cargo").arg("zigbuild")

The following works:

Command::new("cargo").arg("build") 

I asked the rust community, they think this behaviour is weird. This is what they suggested the issue was:

Perhaps they forgot to install this signal handler.

I need the correct behaviour for automating my builds, and allow it to abort on an external signal. thank you!

@cybersoulK
Copy link
Author

cybersoulK commented Oct 5, 2023

oh, my compilation machine OS is mac ventura 13.4 if that's useful.

@messense
Copy link
Member

messense commented Oct 6, 2023

You are welcome to investigate and send PRs to fix it.

@messense messense added the good first issue Good for newcomers label Oct 6, 2023
@konnorandrews
Copy link

konnorandrews commented Oct 6, 2023

For anyone else that encounters this you can use https://docs.rs/command-group/latest/command_group/index.html to spawn cargo-zigbuild in a process group which then can be killed. This is what a terminal does (and why ctrl-c works as expected). It may still be a good idea to add a custom ctrl-c handler to cargo-zigbuild to make this usecase easier.

For those that don't want to use command_group you can use the following.

// Set the process group to the child's process ID (works only on unix systems)
command.process_group(0);

...

// Perform the kill by sending a signal to the *process group* instead of just the one process.
// signal is from the nix crate
signal::killpg(Pid::from_raw(child.id() as _), Signal::SIGINT).unwrap(); // note the signal used here can be changed if needed.

// continue with child cleanup like .wait()

@cybersoulK
Copy link
Author

cybersoulK commented Oct 6, 2023

This worked! Thank you so much!

I forked zigbuild and i tried for several hours, but i don't have enough experience to make this feature work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants