Skip to content

Command substitution leaks processes if they expect input (command substitutions run in their own process group) #1362

Closed

Description

Example: set -l var (cat)

Expected: $var contains value of user input.

Actual: $var contains an empty string, and if you run jobs, a stopped but not reaped cat process lingers unless you run kill -9 on it.

I understand that in this case, I could use read, but this error would still be a thing.

Activity

  1. added this to the fish-future milestone on Mar 26, 2014
  2. ridiculousfish commented on Mar 27, 2014

    @ridiculousfish
    Member

    In bash and zsh, command substitutions run in the process group of the shell. In fish, they run in their own process group. So when they try to read from stdin, they get SIGTTIN or whatever, and stop. Then fish notices they stopped and just continues on, allowing the command substitution to return.

  3. zanchey commented on Mar 29, 2014

    @zanchey
    Member
  4. dimonomid commented on Mar 29, 2014

    @dimonomid

    zanchey, thanks for pointing me to this issue.

    So yes, as I already stated in the mailing list, I'm unable to use fzf in fish shell, and this makes fish completely out for me (fzf is SO useful)

    ridiculousfish, from your message I would assume this behavior can't be changed by definition, like "it's feature, not bug". Am I right? Or, will it be fixed?

  5. neersighted commented on Jun 23, 2014

    @neersighted
    Contributor

    I'd also like to bump this issue. I'm seeing similar problems using the same program (fzf), but this also applies in general to any program that needs to accept input and output to stdout.

    /cc @junegunn

  6. lilyball commented on Sep 15, 2014

    @lilyball
    Contributor

    @ridiculousfish What is the reason behind command substitutions running in their own process group?

  7. added
    bug
    on Sep 15, 2014
  8. ByScripts commented on Nov 17, 2014

    @ByScripts
    Contributor

    Is there any workaround for this ?

    I would like to store the result of a mysql query with set result (mysql -uroot -p -e "...") but since the password is never requested, it does not work.

  9. zanchey commented on Jan 16, 2015

    @zanchey
    Member

    fzf recommends a workaround which may be helpful in some instances - see the fzf README for an example.

  10. paulcarey commented on Jan 16, 2015

    @paulcarey

    Rather than calling $ vim (git ls-files | peco) which fails for the reason described above, xargs can be used to the same effect.

     $ git ls-files | peco | xargs -I _ fish -c 'vim _ < /dev/tty'
    

    I'd guess that the original issue with set could be worked around with a wrapper function that set a universal variable, but I haven't tried this.

  11. jcelliott commented on Jan 27, 2015

    @jcelliott
    Contributor

    Another vote for finding a way to fix this. Is it something that can reasonably be changed?

  12. ridiculousfish commented on Jan 27, 2015

    @ridiculousfish
    Member

    Oh sure, we just have to fix it.

  13. jcelliott commented on Jan 28, 2015

    @jcelliott
    Contributor

    Any pointers on where to start looking?

  14. 22 remaining items

  15. faho commented on Mar 29, 2017

    @faho
    Member

    This is really a UI question - what ought fish to do if a command substitution stops without exiting?

    @ridiculousfish: Honestly, the UI question comes later. The first thing we ought to do is remove the reason these things are stopping in the first place - because they got SIGTTOU, because we don't allow that they use the terminal. That's the most common reason for stopping, so if we remove that, 99% of use for that UI disappears.

    Note also that currently echo (cat | string trim) will hang with no obvious way out because we wait on that forever.

    I'll PR a possible fix for this now.

  16. added 2 commits that reference this issue on Mar 29, 2017
    1d50f31
    b5a38ca
  17. modified the milestones: , fish-future on Apr 10, 2017
  18. added 2 commits that reference this issue on Apr 17, 2017
    5cf4894
    4f2fe8e
  19. JoshCheek commented on Apr 18, 2018

    @JoshCheek

    I hit this issue again both yesterday and today. I didn't reread the thread, the last comment makes it look like this might be a known deficiency, but I reported it here as it seems like a regression. After some debugging, I was able to identify that the issue occurs when I pipe bash -i into a function. SS included to make it easier to read, source included to make it easier to reproduce.

    screen shot 2018-04-18 at 4 04 39 pm

    # ===== Demo the issue =====
    
    # hangs (I'll press C-c)
    $ bash -i -c 'echo hello >&2' | grep whatever
    Job 3, '$ bash -i -c 'echo hello >&2' |…' has stopped
    
    # works (use the command instead of the function)
    $ bash -i -c 'echo hello >&2' | command grep whatever
    hello
    
    # works (remove the -i flag)
    $ bash -c 'echo hello >&2' | grep whatever
    hello
    
    
    # ===== Other potentially useful info =====
    
    # the grep function
    $ type grep
    grep is a function with definition
    function grep
        command grep --color=auto $argv
    end
    
    # the fish version
    $ fish --version
    fish, version 2.7.1
    
    # this is as up-to-date as is installable
    $ brew upgrade fish
    Error: fish 2.7.1 already installed
  20. JoshCheek commented on Apr 18, 2018

    @JoshCheek

    Oh. One other maybe relevant thing: I pressed C-c, but it really stopped the process. ps shows a "T" (I assume this is SIGSTOP) both before and after I press C-c. After I fg, it prints "hello" and exits normally.

    This kind of implies to me that it could be a different issue (b/c it resumes when I foreground it, but the the original had to be SIGKILLed, I believe). Let me know if you want me to post this as its own issue.

  21. faho commented on Apr 19, 2018

    @faho
    Member

    This kind of implies to me that it could be a different issue

    Very much a different issue - this one is explicitly about command substitutions - echo (this stuff).

    The problem with bash -i seems to be that it grabs the terminal for itself - strace -e ioctl shows it doing one with TIOCSPGRP - which is the underlying syscall for tcsetpgrp on linux.

  22. JoshCheek commented on Apr 19, 2018

    @JoshCheek

    Very much a different issue - this one is explicitly about command substitutions - echo (this stuff).

    Okay, made a new issue for it here

  23. locked as resolved and limited conversation to collaborators on Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions