Skip to content

psub never uses mkfifo, but fixing this causes problems #2052

Closed

Description

@waterhouse

So, you may notice that whenever you use psub, it creates a file and puts all the input into that file before anything reads from the file. It turns out that psub has a -f option that causes it to use a file rather than a mkfifo, and it's intended to be capable of both behaviors. However, the code has a simple typo that causes it to act like -f is always chosen. The variable use_fifo defaults to 1, but the test compares 1 with the string "use_fifo" rather than the variable.

--- a/share/functions/psub.fish
+++ b/share/functions/psub.fish
@@ -50,7 +50,7 @@ function psub --description "Read from stdin into a file and output the filename
                set TMPDIR /tmp
        end

-       if test use_fifo = 1
+       if test $use_fifo = 1
                # Write output to pipe. This needs to be done in the background so
                # that the command substitution exits without needing to wait for
                # all the commands to exit

This is an obvious and simple fix; I've put it in my fork, and I would create a pull request for it. However, it creates problems. Those of you who are familiar with issue #206 may not be too surprised to learn that an incoming pipe, created inside a function, which then gets backgrounded, leads to problems.

    if test $use_fifo = 1
        # Write output to pipe. This needs to be done in the background so
        # that the command substitution exits without needing to wait for
        # all the commands to exit
        set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
        set filename $dir/psub.fifo
        mkfifo $filename
        cat >$filename &
    else
        set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
        cat >$filename
    end

The specific symptom is this:

~> wc (head -c 20000 /dev/urandom | psub)
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C
^D
^C^C^C^C
# must resort to external means to kill it

Note that if you replace the 20000 with 10000, or if you run it with -f, it works:

~> wc (head -c 10000 /dev/urandom | psub)
      36     312   10000 /var/folders/s2/s2s+ispYF0WnrMrzB25wok+++TI/-Tmp-//.psub.D42XZSnItG/psub.fifo
~> wc (head -c 20000 /dev/urandom | psub -f)
      84     648   20000 /var/folders/s2/s2s+ispYF0WnrMrzB25wok+++TI/-Tmp-//.psub.GfUK7MPap3

I expect that the size of the pipe that my system uses is 16K, so that head -c 10000 will write all its output to the pipe and exit before something happens to head, whereas head -c 20000 will stick around. What is it that happens to head? Well, let's try this:

~> cat (cat | psub)
a
b
c
^C⏎
~> jobs
Job Group   State   Command
2   83200   stopped cat | psub
3   83210   running cat >$filename &
~> fg
Send job 2, “cat | psub” to foreground
cat: stdin: Interrupted system call

Seems familiar. The stuff in the (... | psub) expression gets stopped. Why? It gets backgrounded, and hit with a SIGTTIN:

~> cat (./signal-me-timbers | psub)
Help help 21
Help help I'm being repressed! 21

(signal-me-timbers is a useful little test program, which acts like cat but will tell you about any signals it receives. man signal will tell you that 21 is indeed SIGTTIN.)

What else can I tell you? Well, that if you run fish --debug 5, you get this repeating infinitely:

fish: select_try on 6
fish: select_try hit timeout
fish: select_try on 6
fish: select_try hit timeout
fish: select_try on 6
fish: select_try hit timeout

Anyway, a solution to this problem seems likely to be related to a solution to #206. It seems like either you could find a way that (... | psub) doesn't get hit with a SIGTTIN, or you could send it a SIGCONT (because the default handler for SIGTTIN is to stop the process). The former would certainly be cleaner...

Activity

  1. zanchey commented on May 5, 2015

    @zanchey
    Member

    Yes - see #1040, which I think describes the problems you have mentioned. If it's ok I will close as a duplicate of that.

  2. waterhouse commented on May 5, 2015

    @waterhouse
    ContributorAuthor

    Ooh, I see. Heh heh, yes, this is a duplicate. Go ahead.

  3. locked as resolved and limited conversation to collaborators on Apr 18, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions