Skip to content

Try again: in unix like system, set foreground process while running external command - #6273

Merged
fdncred merged 12 commits into
nushell:mainfrom
WindSoilder:fg_process_again
Aug 18, 2022
Merged

fdncred merged 12 commits into
nushell:mainfrom
WindSoilder:fg_process_again

Conversation

@WindSoilder

Copy link
Copy Markdown
Contributor

Description

Relative: #6156 #3861 #6240

Check the implementation in ion

I've done running unit testing on unix several times, and done some manual tests, and it works for me.

@herlon214 Would you please check if the build fix the issue #6240 ?

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

@mxfp0

mxfp0 commented Aug 9, 2022

Copy link
Copy Markdown
Contributor

@WindSoilder looks like your branch is working fine here 🎉

@WindSoilder
WindSoilder marked this pull request as ready for review August 9, 2022 23:14
@fdncred

fdncred commented Aug 10, 2022

Copy link
Copy Markdown
Contributor

I tested this too on my mac and couldn't get it to suspend like it was doing.

@fdncred
fdncred requested a review from sophiajt August 10, 2022 12:34
@fdncred

fdncred commented Aug 10, 2022

Copy link
Copy Markdown
Contributor

@jntrnr I requested your review here because you had some way of reproducing some failure with testing. Can you please repeat that on this PR to see if this PR fixes the issues you were seeing?

@sophiajt

Copy link
Copy Markdown
Contributor

Testing it out on my machine, it looks like this doesn't crash anymore which is great.

I'm trying to get the functionality to work in GNOME Terminal: if I type vim foo.txt I'd expect to see that as part of the title, right? This is how it works in fish, but with your changes the title doesn't seem to change. Am I correct that this should be changing?

This change:
Screenshot from 2022-08-12 06-32-40

Fish:
Screenshot from 2022-08-12 06-33-13

@fdncred

fdncred commented Aug 11, 2022

Copy link
Copy Markdown
Contributor

@jntrnr I believe updating the title is a separate thing since right now that is done via ansi escape sequences. I thought this was more for the tmux usage. This issue #6156 goes into detail about what the use case is because I kept pushing back on saying that we can just set the title easily before I understood what the goal was. However, we could throw the escape code in there when this happens too, if we wanted to.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

I thought this was more for the tmux usage

Yeah, and I've checked ion, it doesn't set title as well

@fdncred

fdncred commented Aug 13, 2022

Copy link
Copy Markdown
Contributor

@rosshadden can you please test this to make sure it works as described in #6156?

@rawsp33d

Copy link
Copy Markdown

Yeah I'll try when I can. I just cut my thumb and got stitches tonight so it might have to wait a day or so 😅

@fdncred
fdncred merged commit 1d18f69 into nushell:main Aug 18, 2022
@WindSoilder
WindSoilder deleted the fg_process_again branch August 18, 2022 11:57
@rawsp33d

Copy link
Copy Markdown

A bit late since you merged it a couple hours ago, but I can confirm this works too. Props for getting it working again while fixing the issue that was found with the other approach!

@fdncred

fdncred commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

thanks @rosshadden. we appreciate your feedback and testing!

@dandavison

dandavison commented Aug 30, 2022 •

Copy link
Copy Markdown
Contributor

I've come across a bug introduced by this commit (1d18f69). The only way I currently know to reproduce it involves less, e.g.

yes | head -n 999 | with-env [LESS ""] { less }

Assuming you don't have a very large screen, that command should leave the less process running. It should exit with SIGINT the q keyboard shortcut, and it does on the previous commit (df3b6d9), but does not on 1d18f69.

(Edited)

I'm using less 600 on MacOS.

@fdncred

fdncred commented Aug 30, 2022

Copy link
Copy Markdown
Contributor

@dandavison How can you tell if it's exiting with SIGINT? or are you just saying it doesn't respond to Ctrl-C?

@dandavison

dandavison commented Aug 30, 2022 •

Copy link
Copy Markdown
Contributor

I'm sorry, my initial report was confused: the bug is actually that it doesn't respond to the q keyboard shortcut of less, which should cause the less process to exit (and is what users will usually use).


For signals, the behavior is the same in bash and zsh and nushell (1d18f69) and so there is no bug relating to the way it handles signals. So the following is all irrelevant but, for the record, on bash, zsh, and nushell, it does not respond to ctrl-c or SIGINT (e.g. /bin/kill -s INT or kill -s 2 using nushell's built-in), but it does exit with KILL (e.g. /bin/kill -s KILL or kill -s 9 in nushell).

@fdncred

fdncred commented Aug 30, 2022

Copy link
Copy Markdown
Contributor

hmmm, i don't have a problem with less 590 responding to q with your example.

@dandavison

dandavison commented Aug 30, 2022 •

Copy link
Copy Markdown
Contributor

@fdncred, hm, I certainly hope I'm not wasting time here! But I do still think there's a bug somewhere. I just tested on two separate MacOS machines. With a nu binary built on current main (f1d72e2), the bug reproduced on both machines. I tested less 590 and less 600 on one of those machines and it reproduced for both less versions. (Also checked both iTerm2.app and Terminal.app on one of the machines).

@valpackett

valpackett commented Sep 18, 2022 •

Copy link
Copy Markdown
Contributor

I think the missing piece might be not unblocking signals in the forked child (pre_exec). ion does it here (for a whole forked shell pipeline; only INT/HUP/TERM o_0), also see glibc's shell writing manual

@dandavison

Copy link
Copy Markdown
Contributor

@unrelentingtech thanks for looking into this. I haven't experimented with what you said yet. However, the bug seemed to be present in ion also (@WindSoilder and I both observed that I believe).

@valpackett

valpackett commented Sep 18, 2022 •

Copy link
Copy Markdown
Contributor

Looking at fish, here it actually resets signals for the forked child before launch. It's not super obvious at a first glance but it's a sigprocmask(SIG_SETMASK) with a set that's normally empty or has INT+QUIT for bg jobs when job control is off. Note that SIG_SETMASK is key here, that's why the previously blocked signals get unblocked.

}
}

// If `prepare_to_foreground` function is not called, the function will fail with silence and do nothing.

@valpackett valpackett Sep 18, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure how that comment is true btw? The check is for stdin being a tty; prepare_to_foreground does not touch stdin at all…

It's set_terminal_leader in the shell initialization that does that.

}

// TODO: investigate if we can set foreground process through windows system call.
#[cfg(target_family = "windows")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The blank impl probably should be cfg(not(unix)) rather than windows specifically…

@valpackett

Copy link
Copy Markdown
Contributor

Turns out there's some signal manipulation happening in std::process::Command, it probably was unblocking them. Either way I'm trying to add more stuff from the GNU manual now. Getting interesting but not correct results so far.

valpackett added a commit to valpackett/nushell that referenced this pull request Sep 19, 2022
valpackett added a commit to valpackett/nushell that referenced this pull request Sep 19, 2022
valpackett added a commit to valpackett/nushell that referenced this pull request Sep 20, 2022
fdncred added a commit that referenced this pull request Sep 29, 2022
* Revert "Revert "Try again: in unix like system, set foreground process while running external command (#6273)" (#6542)"

This reverts commit 2bb367f.

* Make foreground job control hopefully work correctly

These changes are mostly inspired by the glibc manual.

* Fix typo in external command description

* Only restore tty control to shell when no fg procs are left; reuse pgrp

* Rework terminal acquirement code to be like fish

Co-authored-by: Darren Schroeder <[email protected]>
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