Skip to content

Piping interactive bash to a function stops the process #4929

Closed

Description

@JoshCheek

I initially reported this issue here.

Description

When I pipe bash -i into a function (in this case, grep), the process gets stopped. @faho did some investigating and said:

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.
-- #1362 (comment)

My environment

$  fish --version
fish, version 2.7.1

$ echo $version
2.7.1

$ uname -a
Darwin Joshs-MacBook-Air-2.local 15.6.0 Darwin Kernel Version 15.6.0: Mon Aug 29 20:21:34 PDT 2016; root:xnu-3248.60.11~1/RELEASE_X86_64 x86_64

$ echo $TERM
xterm-256color

Demo / how to reproduce

# ===== Setup =====

# get a clean environment
$ sh -c 'env HOME=$(mktemp -d) fish'
Welcome to fish, the friendly interactive shell

# shorten the prompt
josh@Joshs-MacBook-Air-2 /U/josh> function fish_prompt; echo '> '; end


# ===== Demo the issue =====

# hangs (I'll press C-c)
> bash -i -c 'echo hello >&2' | grep whatever
Job 1, 'bash -i -c 'echo hello >&2' | g…' has stopped

# its state is "stopped"
> jobs
Job	Group	State	Command
1	24863	stopped	bash -i -c 'echo hello >&2' | grep whatever

# when I foreground it, it completes as expected
> fg
Send job 1, 'bash -i -c 'echo hello >&2' | grep whatever' to foreground
hello


# ===== Slight variations that don't exhibit the issue =====

# use the grep command instead of the function
> bash -i -c 'echo hello >&2' | command grep whatever
hello

# run bash without 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

Activity

  1. faho commented on Apr 19, 2018

    @faho
    Member

    Okay, this is another issue related to #4540, #4653.

    • Fish starts a "keepalive" process that gets its own process group.

    • Fish starts bash in the keepalive's pgroup.

    • Fish goes on to start grep, and because it is inside a function loses track of the old job and starts it in a new job and hence its own pgroup. It assigns control of the terminal to grep.

    • With "-i", bash grabs control of the terminal for its pgroup, away from grep.

    • Bash doesn't read from or write to the terminal

    • grep does, and receives SIGTTOU, which stops it

    • The pipeline hangs

  2. JoshCheek commented on Apr 19, 2018

    @JoshCheek
    Author

    Oh, note that in my example the bash process writes to stderr, and grep writes to stdout. It's leftover context from the initial process I was debugging.

    I just checked, and it doesn't seem to matter, though, this has the same issue:

    $ bash -ic '' | grep x
  3. JoshCheek commented on Apr 19, 2018

    @JoshCheek
    Author

    Another example to make the function thing clearer:

    # Start: iterate over each line of input and print it out.
    # There is no input, so it completes the iteration and exits
    $ bash -ic '' | ruby -e '$stdin.each_line { |l| p l }'
    
    # Now we put the RHS into a function
    $ function x; ruby -e '$stdin.each_line { |l| p l }'; end
    
    # When we pipe to that function instead, it hangs.
    # When I press C-c, it sends an interrupt, Ruby dies, something gets stopped
    $ bash -ic '' | x
    ^CTraceback (most recent call last):
    	1: from -e:1:in `<main>'
    -e:1:in `each_line': Interrupt
    Job 6, 'bash -ic '' | x' has stopped
  4. benitogf commented on Jun 11, 2018

    @benitogf
  5. faho commented on Jun 11, 2018

    @faho
  6. benitogf commented on Jun 12, 2018

    @benitogf
  7. added
    bug
    on Sep 22, 2018
  8. added this to the fish-future milestone on Sep 22, 2018
  9. faho commented on Oct 25, 2018

    @faho
    Member

    This seems to be fixed via #5219.

  10. modified the milestones: fish-future, fish-3.0 on Oct 25, 2018
  11. 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