Skip to content

When called from another shell script, fish can't read #3805

Closed

Description

@nrnrnr
  • Have you checked if problem occurs with fish 2.4.0?
  • Tried fish without third-party customizations (check sh -c 'env HOME=$(mktemp -d) fish')?

fish version installed (fish --version): fish, version 2.3.1

OS/terminal used: Debian Linux (jessie), xterm

I wrote a script using #!/usr/bin/fish. The script calls read. Works fine from command line but not when called from another script.

Reproduction steps

nr@homedog ~/net> call-fish-read 
fish: Couldn't grab control of terminal
tcsetpgrp: Interrupted system call

nr@homedog ~/net> fish-read
say something⏎                                                                  asdf

asdf
nr@homedog ~/net> /usr/bin/fish --version
fish, version 2.3.1-dirty

Scripts are attached in a zip file, but it may be easier to view source here.

Here is fish-read:

#!/usr/bin/fish

read -p 'echo -n say something >&2 ' -l -s answer
echo $answer

and here is call-fish-read:

#!/bin/ksh

mumble=$(fish-read)
echo $mumble

N.B. I downloaded a nightly of fish 2.4.0 from SUSE, and behavior is even more frustrating---the shell just hangs trying to read file descriptor 3. Not sure what's up with that (I poked at it with strace -f.)

Would love to have a workaround that enables me to use a fish script as a tool in other scripts.
demo-scripts.zip

May be related to #3223 and #1258.

Activity

  1. krader1961 commented on Feb 3, 2017

    @krader1961
    Contributor

    This is definitely broken. So far I've only gotten as far as finding that it is blocked inside the call to reader_push() from read_interactive(). I'm very surprised no one has noticed this before.

  2. krader1961 commented on Feb 3, 2017

    @krader1961
    Contributor

    I dug a little deeper. Fish is hanging on the tcsetpgrp() and tcsetattr() calls in reader_interactive_init(). If I just comment those out it gets much farther and reports

    <W> fish: Current terminal parameters have rows and/or columns set to zero.
    <W> fish: The stty command can be used to correct this (e.g., stty rows 80 columns 24).
    

    It then hangs and you still can't type anything and press [enter] to have it read and echoed. But you can press [ctrl-C] and it will terminate the read.

    Basically, fish's handling of the controlling tty, process groups, etc. has a number of quirks. Issue #1258 is definitely relevant. See also #2980.

  3. faho commented on Mar 31, 2017

    @faho
    Member

    So, here's what I found out:

    • When fish starts, it tries to get into its own process group (setpgid in reader_interactive_init)

    • After this, it tries to get this group control of the terminal (tcsetpgrp)

    • That fails if there is still a foreground group that fish is not a member of, by getting a SIGTTOU, which will (unless otherwise handled) stop the process (i.e. fish)

    • At least in my specific case, the old group continues to stay in the foreground, so if fish hadn't switched there wouldn't be an (apparent) problem

    • Process groups are removed when no process in them remains, so there should be something left in the old one, presumably the shell that started fish - this also explains why it works with fish, since that gives the processes it starts their own foreground group

    So I see two things we could do here:

    • Don't switch process groups - I can't see a reason for it and it seems to fix at least this case, maybe every one of these

    • Only perform the tcsetpgrp if there is no (other) foreground group (in that case tcgetpgrp returns "some value larger than 1 that is not presently a process group ID") - in this case we'd have to stop doing stuff we can only do if we're in control

    @krader1961: Any opinion?

  4. zanchey commented on Jun 19, 2017

    @zanchey
    Member

    Interestingly the code to start a new process group and grab control of the terminal was removed from fish_pager in 264c065 back in 2006.

    Otherwise this behaviour dates back to before the source was under revision control.

    I can't see a strong reason that fish tries to start its own process group; the launching process should do that.

    There's definitely some confusion in the code; once again shell_pgid is set to a PID, not a PGID. Perhaps the setpgid line was included to fix failing tcsetpgrp calls when handed a PID rather than a PGID??

    I suggest we remove the process group switching and trial it in master for a time.

  5. faho commented on Jun 20, 2017

    @faho
    Member

    I suggest we remove the process group switching and trial it in master for a time.

    Yes, please!

  6. jlsjonas commented on Aug 14, 2017

    @jlsjonas

    Has there been any progress on this? it's quite an annoying behaviour when (what's presumably is causing the issue in #4261 ) fish is set as your default shell

  7. mqudsi commented on Aug 20, 2017

    @mqudsi
    Contributor

    I just ran into a related issue with the tcsetpgrp and tcsetattr in reader_interactive_shell under WSL. This one is a real head scratcher: microsoft/WSL#1653

    The same code worked before and after recompiling, it doesn't now.

    I've commented out both the tcsetpgrp and tcsetattr calls locally, and would like to trial this in master as discussed above. From my previous work with job control on fish' end, all the child process has to do is try to read or write from/to the terminal and the kernel will take care of the rest, either aborting the process with SIGTTIN or receiving SIGTTOU and hanging until the parent process gives it control of the terminal.

    If this takes care of BashOnWindows#1653 and #3805, maybe we can give it a try?

    I'll check other shells to see if they have similar code or if they just rely on the parent process to do the right thing.

  8. added a commit that references this issue on Aug 21, 2017
    46b5bb2
  9. added 2 commits that reference this issue on Sep 8, 2017
    952a545
    55b3c45
  10. modified the milestones: fish-future, on Jan 11, 2018
  11. IsaacOscar commented on Mar 10, 2018

    @IsaacOscar
  12. 19 remaining items

  13. added 8 commits that reference this issue on Oct 15, 2018
    5ca50bd
    0e00af6
    5dcec40
    3015b45
    e05f1be
    67165bb
    6184c15
    475be24
  14. added 2 commits that reference this issue on Oct 27, 2018
    af0c8d5
    928fc94
  15. added a commit that references this issue on Nov 13, 2018
  16. added 2 commits that reference this issue on Nov 24, 2018
    3674eeb
    cee790e
  17. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions