Skip to content

Do not attempt to take control of terminal in non-interactive mode - #9693

Merged
fdncred merged 11 commits into
nushell:mainfrom
IanManske:acquire-tty-fix
Jul 17, 2023
Merged

fdncred merged 11 commits into
nushell:mainfrom
IanManske:acquire-tty-fix

Conversation

@IanManske

@IanManske IanManske commented Jul 15, 2023 •

Copy link
Copy Markdown
Member

Description

Fixes a regression from #9681 where nushell will attempt to place itself into the background or take control of the terminal even in non-interactive mode.

Using the same reference from #6584:

A subshell that runs interactively has to ensure that it has been placed in the foreground...

A subshell that runs non-interactively cannot and should not support job control.

fish code also seems to follow this.

This partially fixes 9026. That is, nushell will no longer set the foreground process group in non-interactive mode.

@IanManske
IanManske marked this pull request as ready for review July 15, 2023 01:08
@IanManske
IanManske marked this pull request as draft July 15, 2023 01:11
@sophiajt

Copy link
Copy Markdown
Contributor

@IanManske - how do you want to go with this? I've got the revert at #9694 in case we can't come up with a quick fix, but if you think you've got it, we could try your latest changes.

@IanManske

Copy link
Copy Markdown
Member Author

@jntrnr Whoops, forgot to remark this as ready for review. Yes, I think this should work, thanks.

@IanManske
IanManske marked this pull request as ready for review July 15, 2023 01:28
@sophiajt

Copy link
Copy Markdown
Contributor

@IanManske - unfortunately this PR doesn't fix the issue. I went ahead and reverted the original fix to give us time to find a better permanent fix.

@IanManske

Copy link
Copy Markdown
Member Author

Wow this is embarrassing, I must have ran the tests in nushell and not bash on my end. Sorry about that! 😓
I'll revisit this tomorrow.

@IanManske
IanManske marked this pull request as draft July 15, 2023 03:47
@IanManske

Copy link
Copy Markdown
Member Author

Ok, problem was that since we no longer take control of the terminal, we must also not give control of the terminal in non-interactive mode. I ran the tests locally in bash this time 😅, and this seems to fix the issue. I will to continue test for a little longer to make sure. Others might want to confirm that tests work locally in a terminal on macOS, wsl, and windows.

In the reference you can see that they first check if (shell_is_interactive) before calling setpgid or tcsetpgrp.

@sophiajt

Copy link
Copy Markdown
Contributor

I just gave the latest fixes a go, and was able to run a full cargo test.

@IanManske

Copy link
Copy Markdown
Member Author

Ok, great!
I'm going to take a break and come later back to see if there are any edge cases I missed.
But I can mark it ready now if you want.

@fdncred

fdncred commented Jul 15, 2023 •

Copy link
Copy Markdown
Contributor

I checked out this PR and ran cargo test and cargo nextest run --all --all-features without issue on my mac.

@sophiajt
sophiajt marked this pull request as ready for review July 15, 2023 20:45
@IanManske

IanManske commented Jul 16, 2023 •

Copy link
Copy Markdown
Member Author

Ok, I compared bash's pid/ppid/pgid handling to nushell under this PR, and it seems to match in most cases.
The only difference I was able to find is when we run nushell non-interactively through a script, and the script then runs another nushell interactively.

If we add the following lines from the reference to acquire_terminal:

/* Put ourselves in our own process group.  */
shell_pgid = getpid ();
if (setpgid (shell_pgid, shell_pgid) < 0)
  {
    perror ("Couldn't put the shell in its own process group");
    exit (1);
  }

/* Grab control of the terminal.  */
tcsetpgrp (shell_terminal, shell_pgid);

Then, we match bash exactly, but I'm not sure if we absolutely need these lines. The PR as is seems to work?

Also, this PR partially fixes 9026. (I updated the decription to match.)

@IanManske

Copy link
Copy Markdown
Member Author

I decided to add the lines above, so nushell should match bash's job management. This is excluding signals (out of scope for this PR) and background jobs (which we don't have). However, adding these lines caused an issue with nushell as a login shell where it would fail with EPERM. I used fish's workaround for this which is to simply ignore EPERM in this case.

Anyways, I think this PR is ready!

Comment thread src/terminal.rs Resolved
@fdncred

fdncred commented Jul 17, 2023

Copy link
Copy Markdown
Contributor

let's give this a try and see if it works better than our last several tries. 😆 Thanks for working with us @IanManske!

@sholderbach

Copy link
Copy Markdown
Member

OK can still get cargo test --workspace to stop something launched from bash (in WSL2)

@IanManske

Copy link
Copy Markdown
Member Author

@sholderbach Shoot, ok. Is it always happening and is it happening on specific test(s)? I've run cargo test --workspace on latest main about 20 times in bash on linux and haven't been able to get anything.

@fdncred

fdncred commented Jul 18, 2023 •

Copy link
Copy Markdown
Contributor

I think I may have just reproduces this in WSL with nushell as my login shell. Something odd is going on here when i run cargo test --workspace.

test format_conversions::tsv::table_to_tsv_text ... ok
test format_conversions::tsv::table_to_tsv_text_and_from_tsv_text_back_into_table ... ok
test format_conversions::tsv::table_to_tsv_text_and_from_tsv_text_back_into_table_using_csv_separator ... ok
test format_conversions::tsv::table_to_tsv_text_skipping_headers_after_conversion ... ok
test format_conversions::xlsx::from_excel_file_to_table ... ok
test format_conversions::xlsx::from_excel_file_to_table_select_sheet ... ok
test format_conversions::xml::table_to_xml_text_and_from_xml_text_back_into_table ... ok
test format_conversions::yaml::convert_dict_to_yaml_with_boolean_key ... ok
test format_conversions::yaml::convert_dict_to_yaml_with_integer_floats_key ... ok
test format_conversions::yaml::convert_dict_to_yaml_with_integer_key ... ok
test no_search_term_duplicates ... ok
test format_conversions::yaml::table_to_yaml_text_and_from_yaml_text_back_into_table ... ok
test signature_name_matches_command_name ... ok
test quickcheck_parse ... ok
test commands::run_external::better_empty_redirection ... ok
test commands::do_::capture_errors_works_for_external_with_pipeline has been running for over 60 seconds
test commands::do_::capture_errors_works_for_external_with_semicolon has been running for over 60 seconds
test commands::for_::failed_for_should_break_running has been running for over 60 seconds
test commands::for_::for_break_on_external_failed has been running for over 60 seconds
test commands::loop_::failed_loop_should_break_running has been running for over 60 seconds
test commands::loop_::loop_break_on_external_failed has been running for over 60 seconds
test commands::try_::external_failed_should_be_caught has been running for over 60 seconds
test commands::while_::failed_while_should_break_running has been running for over 60 seconds
test commands::while_::while_break_on_external_failed has been running for over 60 seconds

I've ran it twice now and it gives the same response after test commands::run_external::better_empty_redirection ... ok

@IanManske

Copy link
Copy Markdown
Member Author

Ok, issue is --testbin should be non-interactive which is a simple fix. I opened another PR.

sholderbach pushed a commit that referenced this pull request Jul 18, 2023
# Description
After #9693, tests will stop and go into the background if run locally
in bash, since nushell is being run in interactive mode even though
`--testbin` is supplied. This makes nushell run in non-interactive mode
if `--testbin` is supplied.
@IanManske
IanManske deleted the acquire-tty-fix branch July 21, 2023 21:10
sophiajt added a commit that referenced this pull request Aug 1, 2023
# 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.
-->
IanManske pushed a commit to IanManske/nushell that referenced this pull request Aug 3, 2023
# Description

This reverts nushell#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 nushell#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.
-->
fdncred pushed a commit that referenced this pull request Aug 4, 2023
# Description
Currently, foreground process management is disabled for macOS, since
the original code had issues (see #7068).
This PR re-enables process management on macOS in combination with the
changes from #9693.

# User-Facing Changes
Fixes hang on exit for nested nushells on macOS (issue #9859). Nushell
should now manage processes in the same way on macOS and other unix
systems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nushell shouldn't do anything job control-y when not in interactive mode

4 participants