mirror of
https://github.com/nushell/nushell.git
synced 2025-08-16 03:47:54 +02:00
Revert 9693 to prevent CPU hangs (#9893)
# Description This reverts #9693 as it lead to CPU hangs. (btw, did the revert by hand as it couldn't be done automatically. Hopefully I didn't miss anything 😅 ) Fixes #9859 cc @IanManske # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
@ -82,10 +82,7 @@ fn main() -> Result<()> {
|
||||
.unwrap_or_else(|_| std::process::exit(1));
|
||||
|
||||
// keep this condition in sync with the branches at the end
|
||||
engine_state.is_interactive = parsed_nu_cli_args.interactive_shell.is_some()
|
||||
|| (parsed_nu_cli_args.testbin.is_none()
|
||||
&& parsed_nu_cli_args.commands.is_none()
|
||||
&& script_name.is_empty());
|
||||
engine_state.is_interactive = parsed_nu_cli_args.interactive_shell.is_some();
|
||||
|
||||
engine_state.is_login = parsed_nu_cli_args.login_shell.is_some();
|
||||
|
||||
@ -147,7 +144,7 @@ fn main() -> Result<()> {
|
||||
);
|
||||
|
||||
start_time = std::time::Instant::now();
|
||||
acquire_terminal(engine_state.is_interactive);
|
||||
acquire_terminal(parsed_nu_cli_args.commands.is_none() && script_name.is_empty());
|
||||
perf(
|
||||
"acquire_terminal",
|
||||
start_time,
|
||||
@ -293,6 +290,7 @@ fn main() -> Result<()> {
|
||||
input,
|
||||
)
|
||||
} else {
|
||||
engine_state.is_interactive = true;
|
||||
run_repl(&mut engine_state, parsed_nu_cli_args, entire_start_time)
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,19 @@
|
||||
#[cfg(unix)]
|
||||
pub(crate) fn acquire_terminal(interactive: bool) {
|
||||
use is_terminal::IsTerminal;
|
||||
use nix::{
|
||||
errno::Errno,
|
||||
sys::signal::{signal, SigHandler, Signal},
|
||||
unistd,
|
||||
};
|
||||
use nix::sys::signal::{signal, SigHandler, Signal};
|
||||
|
||||
if interactive && std::io::stdin().is_terminal() {
|
||||
// see also: https://www.gnu.org/software/libc/manual/html_node/Initializing-the-Shell.html
|
||||
if !std::io::stdin().is_terminal() {
|
||||
return;
|
||||
}
|
||||
|
||||
take_control();
|
||||
take_control(interactive);
|
||||
|
||||
unsafe {
|
||||
// SIGINT and SIGQUIT have special handling above
|
||||
signal(Signal::SIGTSTP, SigHandler::SigIgn).expect("signal ignore");
|
||||
signal(Signal::SIGTTIN, SigHandler::SigIgn).expect("signal ignore");
|
||||
signal(Signal::SIGTTOU, SigHandler::SigIgn).expect("signal ignore");
|
||||
}
|
||||
|
||||
// Put ourselves in our own process group and take control of terminal, if not already
|
||||
let shell_pgid = unistd::getpid();
|
||||
match unistd::setpgid(shell_pgid, shell_pgid) {
|
||||
// setpgid returns EPERM if we are the session leader (e.g., as a login shell).
|
||||
// The other cases that return EPERM cannot happen, since we gave our own pid.
|
||||
// See: setpgid(2)
|
||||
// Therefore, it is safe to ignore EPERM.
|
||||
Ok(()) | Err(Errno::EPERM) => (),
|
||||
Err(_) => {
|
||||
eprintln!("ERROR: failed to put nushell in its own process group");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
let _ = unistd::tcsetpgrp(nix::libc::STDIN_FILENO, shell_pgid);
|
||||
unsafe {
|
||||
// SIGINT and SIGQUIT have special handling above
|
||||
signal(Signal::SIGTSTP, SigHandler::SigIgn).expect("signal ignore");
|
||||
signal(Signal::SIGTTIN, SigHandler::SigIgn).expect("signal ignore");
|
||||
signal(Signal::SIGTTOU, SigHandler::SigIgn).expect("signal ignore");
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,7 +22,7 @@ pub(crate) fn acquire_terminal(_: bool) {}
|
||||
|
||||
// Inspired by fish's acquire_tty_or_exit
|
||||
#[cfg(unix)]
|
||||
fn take_control() {
|
||||
fn take_control(interactive: bool) {
|
||||
use nix::{
|
||||
errno::Errno,
|
||||
sys::signal::{self, SaFlags, SigAction, SigHandler, SigSet, Signal},
|
||||
@ -78,23 +59,32 @@ fn take_control() {
|
||||
}
|
||||
}
|
||||
|
||||
let mut success = false;
|
||||
for _ in 0..4096 {
|
||||
match unistd::tcgetpgrp(nix::libc::STDIN_FILENO) {
|
||||
Ok(owner_pgid) if owner_pgid == shell_pgid => {
|
||||
// success
|
||||
return;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
Ok(owner_pgid) if owner_pgid == Pid::from_raw(0) => {
|
||||
// Zero basically means something like "not owned" and we can just take it
|
||||
let _ = unistd::tcsetpgrp(nix::libc::STDIN_FILENO, shell_pgid);
|
||||
}
|
||||
Err(Errno::ENOTTY) => {
|
||||
if !interactive {
|
||||
// that's fine
|
||||
return;
|
||||
}
|
||||
eprintln!("ERROR: no TTY for interactive shell");
|
||||
std::process::exit(1);
|
||||
}
|
||||
_ => {
|
||||
// fish also has other heuristics than "too many attempts" for the orphan check, but they're optional
|
||||
if signal::killpg(shell_pgid, Signal::SIGTTIN).is_err() {
|
||||
if signal::killpg(Pid::from_raw(-shell_pgid.as_raw()), Signal::SIGTTIN).is_err() {
|
||||
if !interactive {
|
||||
// that's fine
|
||||
return;
|
||||
}
|
||||
eprintln!("ERROR: failed to SIGTTIN ourselves");
|
||||
std::process::exit(1);
|
||||
}
|
||||
@ -102,6 +92,8 @@ fn take_control() {
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("ERROR: failed take control of the terminal, we might be orphaned");
|
||||
std::process::exit(1);
|
||||
if !success && interactive {
|
||||
eprintln!("ERROR: failed take control of the terminal, we might be orphaned");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user