-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Child exit with signal n returns 128+n #10781
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::commands::ExitStatus; | ||
use tokio::process::Child; | ||
use tracing::debug; | ||
|
||
/// Wait for the child process to complete, handling signals and error codes. | ||
pub(crate) async fn run_to_completion(mut handle: Child) -> anyhow::Result<ExitStatus> { | ||
// Ignore signals in the parent process, deferring them to the child. This is safe as long as | ||
// the command is the last thing that runs in this process; otherwise, we'd need to restore the | ||
// signal handlers after the command completes. | ||
let _handler = tokio::spawn(async { while tokio::signal::ctrl_c().await.is_ok() {} }); | ||
|
||
// Exit based on the result of the command. | ||
#[cfg(unix)] | ||
let status = { | ||
use tokio::select; | ||
use tokio::signal::unix::{signal, SignalKind}; | ||
|
||
let mut term_signal = signal(SignalKind::terminate())?; | ||
loop { | ||
select! { | ||
result = handle.wait() => { | ||
break result; | ||
}, | ||
|
||
// `SIGTERM` | ||
_ = term_signal.recv() => { | ||
let _ = terminate_process(&mut handle); | ||
} | ||
}; | ||
} | ||
}?; | ||
|
||
#[cfg(not(unix))] | ||
let status = handle.wait().await?; | ||
|
||
if let Some(code) = status.code() { | ||
debug!("Command exited with code: {code}"); | ||
if let Ok(code) = u8::try_from(code) { | ||
Ok(ExitStatus::External(code)) | ||
} else { | ||
#[allow(clippy::exit)] | ||
std::process::exit(code); | ||
} | ||
} else { | ||
#[cfg(unix)] | ||
{ | ||
use std::os::unix::process::ExitStatusExt; | ||
debug!("Command exited with signal: {:?}", status.signal()); | ||
// Following https://tldp.org/LDP/abs/html/exitcodes.html, a fatal signal n gets the | ||
// exit code 128+n | ||
if let Some(mapped_code) = status | ||
.signal() | ||
.and_then(|signal| u8::try_from(signal).ok()) | ||
.and_then(|signal| 128u8.checked_add(signal)) | ||
{ | ||
return Ok(ExitStatus::External(mapped_code)); | ||
} | ||
} | ||
Ok(ExitStatus::Failure) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn terminate_process(child: &mut Child) -> anyhow::Result<()> { | ||
use anyhow::Context; | ||
use nix::sys::signal::{self, Signal}; | ||
use nix::unistd::Pid; | ||
|
||
let pid = child.id().context("Failed to get child process ID")?; | ||
signal::kill(Pid::from_raw(pid.try_into()?), Signal::SIGTERM).context("Failed to send SIGTERM") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh hey this is exactly the platform-specific code that I was surprised to see when looking at #9652, still not sure why we don't do an equivalent thing on windows...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with how signals work on windows, what do we need to do for windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it's more of a question of: why do we need to explicitly kill the script? Won't killing our own process "normally" in response to CTRL+C achieve the desired effect since it's a child process?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the context you're looking for in
uv run
#6738There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow yep it sure does, thanks!