Skip to content

Foreground process group management, again - #6584

Merged
fdncred merged 6 commits into
nushell:mainfrom
valpackett:pgrp
Sep 29, 2022
Merged

fdncred merged 6 commits into
nushell:mainfrom
valpackett:pgrp

Conversation

@valpackett

@valpackett valpackett commented Sep 19, 2022 •

Copy link
Copy Markdown
Contributor

Description

Continuing: #6273 #6240 #6156 #3861

This changes #6273 to work a lot more like what this manual describes.

As a result, I'm not getting the pipe hang bug which I could reproduce with fd . . | fzy.

There's still some kind of pipe bug though: fd . . | sk sometimes works, sometimes thread 'main' panicked at 'failed to get next char from input', (with some other message blinking quickly). fd . . | fzf just says Failed to read /dev/tty.
(probably just the fact that we're looking at stdin. when stdin is the piped input. we should explicitly carry the tty into the pre_exec?)

In addition… since for now there's no background support I've left SIGTSTP ignored in the spawned child process, so now the habit of Ctrl+Z'ing TUI applications won't cause an irrecoverable frozen mess (unless the application resets signal handling, but most don't. (quick testing: in htop, ncdu, helix, kakoune Ctrl-Z is ignored now; neovim hides its UI but it's possible to Ctrl-C it and not lose the shell)

Tests

Make sure you've done the following:

  • Add tests that cover your changes, either in the command examples, the crate/tests folder, or in the /tests folder.
  • Try to think about corner cases and various ways how your changes could break. Cover them with tests.
  • If adding tests is not possible, please document in the PR body a minimal example with steps on how to reproduce so one can verify your change works.

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 --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace --features=extra to check that all the tests pass

@WindSoilder

Copy link
Copy Markdown
Contributor

Hi, thank you very much for your PR. Relative regions are something I'm not good at and it leaves some weird errors.

Really appreciate the pr to make it better.

@valpackett

valpackett commented Sep 19, 2022 •

Copy link
Copy Markdown
Contributor Author

Oh I see the current problem now: fd becomes foreground → fzf becomes foreground → fd finishes its task and quits, causing the shell to make itself foreground. *facepalm*.

We really have to reuse the pgrp of the first external command in a pipeline like all the classic shells do, no way around that…

Screen_2022-09-19-21:02:15

@valpackett
valpackett force-pushed the pgrp branch 2 times, most recently from 5b567be to 9d3e723 Compare September 19, 2022 23:23
@valpackett

Copy link
Copy Markdown
Contributor Author

Okay. Finally I think it might be correct! At least I'm not seeing any bugs right now. 🎉

It's really difficult to fit this running-pipeline-local state into the core of nushell, which kinda just doesn't want to be a unix shell actually >_< What I settled on was putting the state in EngineState and fully managing it inside the foreground machinery, which turns out is actually possible and lets us avoid actually having a concept of a running job pipeline. The trick is having a counter of running external processes. When it goes back down to zero we can reset the stored pgrp to stop reusing it and put the shell itself into foreground.

Currently this is not the most beautiful code (the bare pair of atomics is a bit ugly and the trickery with the zero value is not very rust-y) but before polishing we need more testing!

Please throw your best test cases at this! Go nuts with pipelines, try to break it!

@valpackett
valpackett force-pushed the pgrp branch 2 times, most recently from e2e6029 to 148d0c5 Compare September 19, 2022 23:44
@dandavison

dandavison commented Sep 20, 2022 •

Copy link
Copy Markdown
Contributor

Hi @unrelentingtech, thanks a lot for working on this. My initial tests look good w.r.t piping commands that were previously broken in MacOS (e.g. yes | head -n 999 | less didn't exit on q).

However, I'm finding that it no longer works as the shell for tmux. To reproduce that, create a ~/.tmux.conf containing the line

set -g default-shell /PATH/TO/nushell/target/release/nu

and then run tmux -v new. That immediately exits for me, printing

[exited]

to the screen, and dumping a log file named tmux-client-*.log, which looks more or less like the log file in this tmux support thread, in particular the last line of it is client loop exit.

@dandavison

Copy link
Copy Markdown
Contributor

Similarly I'm finding that it exits immediately when set as the program for alacritty to run, i.e. in alacritty.yml

shell:
  program: /PATH/TO/nushell/target/release/nu

(I'm using MacOS)

@valpackett

Copy link
Copy Markdown
Contributor Author

I see, it's setpgid().expect() failing. I'm working on it

@dandavison

Copy link
Copy Markdown
Contributor

I confirm that on MacOS this branch results in tmux showing "python" in its status bar when I run python, and that that does not happen on nushell main. (My understanding (see #6542) is that that is the functional difference we expect between main and this branch.)

@valpackett

Copy link
Copy Markdown
Contributor Author

Reworked terminal acquirement code to do basically what fish does 🚀

@dandavison terminal title is a minor benefit of it.
Correct signal handling (e.g. Ctrl-C sending SIGINT to all external processes in the currently running pipeline) is more important — it's kinda what makes a shell a shell and not some random REPL launching external processes somehow.

@dandavison

dandavison commented Sep 20, 2022 •

Copy link
Copy Markdown
Contributor

Reworked terminal acquirement code to do basically what fish does 🚀

Excellent! Confirm works my end under tmux/alacritty.

@dandavison terminal title is a minor benefit of it.
Correct signal handling (e.g. Ctrl-C sending SIGINT to all external processes in the currently running pipeline) is more important — it's kinda what makes a shell a shell and not some random REPL launching external processes somehow.

Haha, yes, I absolutely believe that you that there are more profound benefits of this change than the terminal title! You've probably already thought about this -- and I am not suggesting that this block merging this PR -- but given the history of subtle bugs and slightly broken releases I think it would be really valuable to put in place some tests that verify aspects of correct process and signal handling, etc. I.e. tests that would fail on main now, and pass on this branch. I'm imagining that those would have to be a new style of "integration" test that involves building nu in CI, and running the built binary (I think those don't exist currently?). Do you agree that that would be valuable in principle and would you be able to make some suggestions for what sorts of tests and assertions might be feasible? It's not the easiest area to test but I think it would be great to protect your work against any future mistakes.

@valpackett

Copy link
Copy Markdown
Contributor Author

I really don't have the energy for working on this kind of thing currently :/ but it's probably possible to do something similar to what fish does with pexpect testing. Ideally it would run various pipelines composed of an external test helper program controllable via a side channel, and check who's in the foreground at various points.

@dandavison

Copy link
Copy Markdown
Contributor

it's probably possible to do something similar to what fish does with pexpect testing

That sounds like a great suggestion and looks like very helpful prior art from fish. I've opened
#6591 to capture your idea.

@fdncred

fdncred commented Sep 20, 2022

Copy link
Copy Markdown
Contributor

wow, good work here. these are some pretty significant changes.
"Once more unto the breach!"

@dandavison

dandavison commented Sep 26, 2022 •

Copy link
Copy Markdown
Contributor

One data point: I've been running a nushell build including these commits for the last week and haven't encountered any problems.

@fdncred

fdncred commented Sep 26, 2022

Copy link
Copy Markdown
Contributor

I didn't realize this was ready for review? Is the work complete here?

@dandavison

Copy link
Copy Markdown
Contributor

I didn't realize this was ready for review? Is the work complete here?

It will help to have lots of manual testing of this branch, from users with different workflows. I believe the branch is already in a state where manual testing from users is helpful. Quoting @unrelentingtech from above :)

but before polishing we need more testing!
Please throw your best test cases at this! Go nuts with pipelines, try to break it!

@fdncred

fdncred commented Sep 26, 2022

Copy link
Copy Markdown
Contributor

Since we have a release tomorrow, I propose to wait a couple days and land it. That will give us about 3 weeks to test more thoroughly once it's merged.

@WindSoilder

Copy link
Copy Markdown
Contributor

Hi @rosshadden , sorry for disturb

Can you please verify if this patch works for #6156

@rawsp33d

Copy link
Copy Markdown

Yep @WindSoilder this branch seems to work for me.

@fdncred
fdncred marked this pull request as ready for review September 27, 2022 13:50
@fdncred

fdncred commented Sep 28, 2022

Copy link
Copy Markdown
Contributor

@unrelentingtech if we can get this conflict resolved we can land it.

@fdncred

fdncred commented Sep 29, 2022 •

Copy link
Copy Markdown
Contributor

Here we go, hold your breath and cross your fingers!

@fdncred
fdncred merged commit 4af0a6a into nushell:main Sep 29, 2022
Comment thread src/main.rs
};

let shell_pgid = unistd::getpgrp();
let owner_pgid = unistd::tcgetpgrp(nix::libc::STDIN_FILENO).expect("tcgetpgrp");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am running into a new problem with this change:

Trying to launch interactive nushell via the CodeLLDB VS Code extension causes a panic on this line.

grafik

It appears that fd 0 is a valid tty and the atty crate is reporting the same thing. I don't fully grasp how we can deal with the other error cases vaguely described in https://man7.org/linux/man-pages/man3/tcgetpgrp.3.html

Reproducibility notes: both on Ubuntu standalone and with WSL2 I was able to cause this crash (for the external and internal terminal option in the launch.json config).

Launching gdb target/debug/nu was not picking up this panic if I get this correctly (but my gdb foo is not strong enough to properly launch nu in a separate terminal while being attached from the start.) Regular late attaching with gdb -p <nu pid> was not causing unexpected problems.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, does fish work in the same place?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have to read up on the launch configuration options for that.

@sholderbach sholderbach Oct 17, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a separate issue #6788 for that problem, even if fish were to crash there we shouldn't to enable us to debug other things on startup.

@WindSoilder

Copy link
Copy Markdown
Contributor

Sometimes my tests failed with the following message in the latest main:

failures:

---- tests::test_config_path::test_default_config_path stdout ----
=== stderr

=== stderr
ERROR: failed to SIGTTIN ourselves

thread 'tests::test_config_path::test_default_config_path' panicked at 'assertion failed: `(left == right)`
  left: `""`,
 right: `"/home/rice/.config/nushell/env.nu"`', src/tests/test_config_path.rs:14:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::test_config_path::test_default_config_path

fdncred pushed a commit that referenced this pull request Jul 17, 2023
…9693)

# 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](https://www.gnu.org/software/libc/manual/html_node/Initializing-the-Shell.html)
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](https://github.com/fish-shell/fish-shell/blob/54fa1ad6ec7adb0d9c14076843ef5e51c18ecc7a/src/reader.cpp#L4862)
also seems to follow this.

This *partially* fixes
[9026](#9026). That is, nushell
will no longer set the foreground process group in non-interactive mode.
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.

7 participants