Skip to content

read | cat crashes fish #4540

Closed
Closed

Description

@JohnMH

fish 2.3.1
operating system: Irrelevant, running under Linux kernel (Also tested on FreeBSD with unknown fish version)
term: xterm-256color

Activity

  1. JohnMH commented on Nov 17, 2017

    @JohnMH
    Author

    Sometimes this also closes the terminal emulator.

  2. added
    bug
    on Nov 18, 2017
  3. zanchey commented on Nov 18, 2017

    @zanchey
    Member

    It sure does, even with 2.7b1 - in a subshell the terminal hangs, in it's own window the emulator just closes!

  4. floam commented on Nov 19, 2017

    @floam
    Member

    Doesn't happen here (macOS), for some reason.

  5. gyroninja commented on Nov 27, 2017

    @gyroninja

    Running fish in gdb results with:
    Thread 1 fish received signal SIGTTIN, Stopped (tty input). 0x00007ffff776e15d in read () from /lib/libpthread.so.0

    From the glibc documentation on SIGTTIN:

    A process cannot read from the user’s terminal while it is running as a background job. When any process in a background job tries to read from the terminal, all of the processes in the job are sent a SIGTTIN signal. The default action for this signal is to stop the process. For more information about how this interacts with the terminal driver, see Access to the Terminal.

  6. ridiculousfish commented on Nov 27, 2017

    @ridiculousfish
    Member

    I can't repro the crash but I can repro the hang. Attaching in the debugger, fish just gets SIGTTIN repeatedly. I wonder if this broken when fish stopped blocking signals (#3691).

  7. added this to the milestone on Nov 27, 2017
  8. Asternitix commented on Nov 29, 2017

    @Asternitix

    I can repro the hang as well but the text in the terminal goes black but still works.

  9. gyroninja commented on Nov 29, 2017

    @gyroninja

    I'm using xterm 330 and fish 2.7.0.
    The terminal does not seem to be crashing, but rather closing due to an error:
    https://github.com/joejulian/xterm/blob/master/ptydata.c#L213
    Essentially, the read call is failing with a errno of EIO. From the man page:

    EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling terminal, and either it is ignoring or blocking SIGTTIN or its process group is orphaned.
    It may also occur when there is a low-level I/O error while reading from a disk or tape.

  10. faho commented on Dec 26, 2017

    @faho
    Member

    So, I've checked this a bit, and this is the issue as I understand it:

    For certain cases, fish creates a "keepalive" process. This is used to prevent reuse of the foreground pgroup leader PID, which would grant that new process access to the terminal. (Which in itself is an edgecase's edgecase, but theoretically more sound, so okay)

    However, the check for a keepalive process is this:

        if (j->get_flag(JOB_CONTROL) && !exec_error) {
            for (const process_ptr_t &p : j->processes) {
                if (p->type != EXTERNAL && (!p->is_last_in_job || !p->is_first_in_job)) {
                    needs_keepalive = true;
                    break;
                }
            }
        }

    So, if the job contains any process that isn't "EXTERNAL" (e.g. a builtin) and that process isn't both the first and last in the job (which would itself mean the job has only one process), create a keepalive process.

    This keepalive process then becomes the foreground pgroup leader via set_child_group(j, keepalive.pid).

    But in this case - read is executed in the fish process, so the pgroup that reads isn't the foreground pgroup. So it gets SIGTTIN/EIO/whatever, and that presents as this issue.

    As I understand it, the process that should be allowed to read() from the terminal here is the first in the pipe - the read. Which is fish itself. So we don't actually need set_child_group, and we don't actually need the keepalive process (since fish itself survives).

    And the process that should be allowed to write() to the terminal should be the cat. However IIUC the cat is actually started later since we buffer the read output (I can't actually see a running cat process here), so we don't need to explicitly do anything more.

    TBD is the actual conditions that a keepalive process is necessary.

    And now I'm down the job control rabbit hole again...

  11. added a commit that references this issue on Jan 10, 2018
    ecaed7b
  12. mqudsi commented on Feb 9, 2018

    @mqudsi
    Contributor

    The minimal read | cat test case appears to be fixed with the recent changes:

    image

    But I'd have to try on a non-WSL machine too, for sure.

  13. faho commented on Feb 10, 2018

    @faho
    Member

    Definitely not fixed on Linux.

  14. JohnMH commented on Feb 11, 2018

    @JohnMH
    Author

    @mqudsi I'd be willing to bet that's more due to some issue with signals on Windows.

  15. mqudsi commented on Mar 5, 2018

    @mqudsi
    Contributor

    @JohnMH it was actually due to the extraneous setpgid calls. The changes introduced to fix #4778 cause WSL to similarly hang now.

  16. added a commit that references this issue on Aug 5, 2018
    e6b5f98
  17. added a commit that references this issue on Aug 12, 2018
    fa66ac8
  18. 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