You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
find a way to make psub --fifo safe from deadlock #1040
The easy one is here — use_fifo is missing a sigil and is therefore a string comparison, causing psub to always act as psub -f.
Unfortunately, it's not as simple as fixing that typo as doing so will cause a non-interruptable hang under certain circumstances. I believe it occurs when the pipe buffer is exceeded? But I'm not sure how to actually determine the pipe buffer in fish. Maybe forking another process is needed? Or... something.
Anyway, here's a (hopefully) cross-platform test case (about 1.5MiB and requires Java, uses openssl to decode base64). It's a standalone wrapper for rhino. There's a big hunk of base64 in the middle of it, but the script is just: java -jar (echo 'BIGHUNKOFBASE64' | openssl base64 -d | psub)
Disregard everything in the psub man page.
Virtually unchanged from when it was written 10 years ago for fish 1.0.0, literally (and by literally, I mean literally) every sentence in man psub is wrong (except for the example). With that out of the way...
What is 'process substitution'?
Process substitution (which itself is something of a misnomer) was a concept introduced in the Korn shell (ksh88) as a shorthand for the often messy process of using file descriptors and named pipes with a command or program when the program expects a file as an argument. In other words, it is a way to emulate including these programs in a UNIX pipeline, where one otherwise could not, either simply by design (man dd), or for some other clever reason (man tee). The syntax used by shells that support this is:
command<(process) ...
The standard output ofprocessis fed into a file descriptor or named pipe, which is passed as an argument tocommand.
and
process >(command) ...
The standard input ofcommandis read from whateverprocessis doing with that argument (ideally, producing output).
This syntax is by no means standard, and it certainly is not POSIX. It is supported by ksh88 and ksh93, but not pdksh or mksh; bash supports it, but not in sh mode (bash --posix). It is supported in zsh when not in an emulation mode that proscribes it; and zsh also has another syntax, =(process), which we'll get to in a bit.
The fishpsub function:
command (process |psub)
(purportedly) capitalizes on the fact that no special syntax is needed to perform "process substitution"; the ordinary syntax of command substitution (command or $(command) in Bourne shells, (command) is fish) can be used to accomplish the same thing pipeline within the command substitution whose standard output culminates in a file descriptor or named pipe.
So, why has this bug stayed in place since it was introduced seven years ago (c6ebb23), two years since I opened this issue, followed by about a dozen others?
This is where it gets tricky, and there are two factors at play.
a. Many utilities which take files as arguments (i.e., for input or output), do so for a reason. A buffered UNIX pipeline often simply is not acceptable ... blah blah blah I've been up all night hacking on this so I'll finish my blustering treatise later. 💀
Basically, I propose we use a ramdisk. It combines to the "durability", ability to handle large files and non-streamable data of "file substitution" (?) (zsh =(command), what we're doing with psub at present) with the speed and ephemeral nature of using a fd or fifo.
functionpsub--description"Process substitution, revisited."set-l filename
set-l funcname
set-l halfmem
set-l sectors
set-l ramdisk
set-l mountpoint
set-l psubdir
set-l use_file 0
whileset-q argv[1]
switch$argv[1]
case-h--help__fish_print_helppsubreturn 0
case-f--fileset use_file 1
endset-e argv[1]
endifnotstatus--is-command-substitutionechopsub: Not inside of command substitution >&2return 1
endiftest-z"$TMPDIR"set TMPDIR /tmp
end# Implemention for other systems is left as an exercise for the reader.iftest (uname) != Darwin
set use_file 1 # ... mount -t tmpfs ...endiftest$use_file-eq 1
whilenotset psubdir (mktemp -d$TMPDIR/.psub.XXXXXXXXXX); endchmod 0300 $psubdirwhilenotset filename (mktemp $psubdir/temp.XXXXXXXXXX); endchmod 0100 $psubdirchmod 0200 $filenameelse# Basically: detect memory and use 1/2 of it (the default with tmpfs# on other platforms) as a ramdisk. The memory is allocated as needed.# `hdid -nomount ram://SECTORS` (of 512 bytes) on Darwin.set halfmem (math (sysctl -n hw.memsize) / 2)
set sectors (math$halfmem / 512)
set ramdisk (hdid -nomount ram://$sectors|tr-d [:space:])
chmod 0600 $ramdisk# $ramdisk is now something like '/dev/disk2'; it would be nice if we# could just use the raw device file as $filename, but if we do that# there's no EOF. So we format, mount, and use a tempfile on our# ramdisk. UDF or is probably as good as anything. We probably just# don't want any filesystem that's journaled to reduce overhead.whilenotset mountpoint (mktemp -d /$TMPDIR/.psub.XXXXXXXXXX); endchmod 0300 $mountpoint
newfs_udf $ramdisk>/dev/null 2>&1
mount_udf -o nobrowse $ramdisk$mountpointwhilenotset psubdir (mktemp -d$mountpoint/.psub.XXXXXXXXXX); endchmod 0300 $psubdirwhilenotset filename (mktemp $psubdir/temp.XXXXXXXXXX); endchmod 0100 $mountpointchmod 0100 $psubdirchmod 0200 $filenameend# Write stdin to tempfilecat>$filenamechmod 0400 $filename# Write filename to stdoutecho$filename# Find unique function namewhiletrueset funcname __fish_psub_(random)
ifnotfunctions$funcname>/dev/null 2>&1breakendend# Make sure we unmount and detatch when caller exits.function$funcname--on-job-exit caller --inherit-variable filename --inherit-variable funcname --inherit-variable use_file --inherit-variable ramdisk --inherit-variable mountpoint --inherit-variable psubdir
chmod 0700 $psubdir$filenameiftest$use_file-eq 0
umount -f$mountpoint>/dev/null 2>&1
which hdiutil >/dev/null 2>&1and hdiutil detach $ramdisk>/dev/null 2>&1chmod 0700 $mountpointendcommandrm-rf$mountpoint$psubdir$filenamefunctions-e$funcnameendend
Virtually unchanged from when it was written 10 years ago for fish 1.0.0, literally (and by literally, I mean literally) every sentence in man psub is wrong (except for the example).
I don't think so - it could be improved but I don't see much that is wrong. A bit imprecise and awkward maybe but assuming psub were bug-free it would mostly be sort-of correct. Anyway, this isn't really important to the matter at hand - we should improve psub documentation, but mostly we should just improve psub.
Many utilities which take files as arguments (i.e., for input or output), do so for a reason. A buffered UNIX pipeline often simply is not acceptable
Or try to comm or diff the output of two commands - how would you specify that? Piping syntax doesn't scale beyond a one-to-one relationship (at least I haven't ever seen how that'd work).
... mount -t tmpfs ...
Nope - "mount: only root can use "--types" option". (There is probably a way, but that one's not it)
which hdiutil >/dev/null 2>&1
and hdiutil detach $ramdisk >/dev/null 2>&1
This doesn't seem great - you're creating a ramdisk with one tool and then only detach it if another tool exists?
Anyway, I don't see the merits of this approach for my system - /tmp (where current psub stores its fifo or file) is already a tmpfs. As my cursory googling shows, it's the default on archlinux (my distro), Fedora, Debian (nope, they reverted) and maybe Ubuntu, so other linuxen also won't benefit from this - at all. Even for those that don't have /tmp on tmpfs, we could use /run instead, which is explicitly defined to be one.
Plus, fifos have an advantage that you don't have here - they can be filled in the background, which means the reading side can start earlier (which is presumably why zsh offers both fifos and files). Check the current psub source (try not to step on the bugs) - the fifo path does so, while the file path does not. Also, this increases the code complexity, especially the amount of OS-specific code.
I don't think so - it could be improved but I don't see much that is wrong. A bit imprecise and awkward maybe but assuming psub were bug-free it would mostly be sort-of correct. Anyway, this isn't really important to the matter at hand - we should improve psub documentation, but mostly we should just improve sub.
It is important if you one wants to know what we're trying to accomplish with this. Line-by-line:
Posix shells feature a syntax that is a mix between command substitution and piping, called process substitution.
Posix shells do not feature process substitution.
It is used to send the output of a command into the calling command, much like command substitution, but with the difference that the output is not sent through commandline arguments but through a named pipe, with the filename of the named pipe sent as an argument to the calling program.
Aside from the fact that bash is the only shell which can fall back to using a FIFO (on systems which lack numbered file descriptors), all other shells which implement process substitution use numbered file descriptors, not named pipes. The sentence is also self-contradictory, as it says output is not sent via command line arguments, then goes on to say the filename (the output of the final command in the pipeline) is sent as an argument.
psub combined with a regular command substitution provides the same functionality.
It doesn't, since it exclusively uses named pipes (in theory). But its also wrong because:
If the -f or --file switch is given to psub, psub will use a regular file instead of a named pipe to communicate with the calling process.
Nope, because of the type, this is what it always does, every since that switch was added.
This will cause psub to be significantly slower when large amounts of data are involved, but has the advantage that the reading process can seek in the stream.
Nope, for the reason above, psub and psub -f have identical behavior. Its also not necessarily true in any case, depending on how the stream is buffered, disk throughput, etc.
Every sentence.
On to more relevant matters:
Many utilities which take files as arguments (i.e., for input or output), do so for a reason. A buffered UNIX pipeline often simply is not acceptable
Or try to comm or diff the output of two commands - how would you specify that? Piping syntax doesn't scale beyond a one-to-one relationship (at least I haven't ever seen how that'd work).
You're quoting right where I dropped off there, so I'm not sure we actually disagree here.
But my point here is threefold:
Often, a pipeline lacks the necessary complexity to handle all input and output (which we seem to agree on),
However, pipes of any type, and therefore process substitution by any means (FIFOs, /dev/fd/X, etc) may not (and frequently do not) function in a manner sufficient to produce the same behavior as a regular file, do to buffering, etc..
But notwithstanding the point above, there are a sufficient number of situations where one does not want large, intermediate temporary files on magnetic disks.
which hdiutil >/dev/null 2>&1
and hdiutil detach $ramdisk >/dev/null 2>&1
This doesn't seem great - you're creating a ramdisk with one tool and then only detach it if another tool exists?
No, while I probably don't need this guard line any more as I've since wrapped it in an if block, what I'm doing is handling an annoying fish "feature" which does not allow you to squash the output of attempting to use a command that does not exist.
... mount -t tmpfs ...
Nope - "mount: only root can use "--types" option". (There is probably a way, but that one's not it)
Anyway, I don't see the merits of this approach for my system - /tmp (where current psub stores its fifo or file) is already a tmpfs. As my cursory googling shows, it's the default on archlinux (my distro), Fedora, Debian (nope, they reverted) and maybe Ubuntu, so other linuxen also won't benefit from this - at all. Even for those that don't have /tmp on tmpfs...
Perhaps you missed my joke:
# Implemention for other systems is left as an exercise for the reader.
That is to say, it's likely already implemented. As in, when your init scripts ran mount -t tmpfs as root.
Plus, fifos have an advantage that you don't have here - they can be filled in the background, which means the reading side can start earlier (which is presumably why zsh offers both fifos and files).
There's nothing preventing one from reading a regular file while its still being written. tail -f?
The difference is a fifo is buffered, which is also why so many programs fail with fifos.
Also, this increases the code complexity, especially the amount of OS-specific code.
There is ample precedent for this. There is an immense ammount of OS-specific code in fish. ls.fish. open.fish. How man __fish_systemctl_SOMETHING.fish functions are there?
Can't say I'm a fan.
Well... sorry, I guess? We can't all run Arch Linux.
And I must say, pretty rude IMO, considering I only did any of this in light of the fact that you specifically asked for "progress" on this issue. I guess I interpreted that to mean more meaningful/fundamental improvements, since the forking problem is a much larger issue, well beyond the scope of this here..
Aside from the fact that bash is the only shell which can fall back to using a FIFO (on systems which lack numbered file descriptors), all other shells which implement process substitution use numbered file descriptors, not named pipes.
Sure that zsh does it that way?
Nope, because of the type, this is what it always does, every since that switch was added.
"assuming psub were bug-free".
You're quoting right where I dropped off there, so I'm not sure we actually disagree here.
We don't - I was expanding on your point.
handling an annoying fish "feature" which does not allow you to squash the output of attempting to use a command that does not exist
Ah okay. In that case, shouldn't you do that with anything? Or isn't the error output here kinda important? You're leaking tmpfss (if I understand correctly).
Perhaps you missed my joke:
I was trying to say that it might be more complicated for other systems, though now I see that we probably could use /run on linux and just do the ramdisk setup on OSX/BSD.
There's nothing preventing one from reading a regular file while its still being written. tail -f?
Maybe we should consider running the cat > file in the background then, too?
There is ample precedent for this.
This seems a bit more complicated than most OS-specific paths.
How man __fish_systemctl_SOMETHING.fish functions are there?
For the record, I'm a bit annoyed by those, mostly since most of them are only used by the systemctl completion, AFAIK (I've thought about moving them into that, but I wanted to look into why they were moved out). Also, this is in completions, which are much less critical than psub.
And I must say, pretty rude IMO, considering I only did any of this in light of the fact that you specifically asked for "progress" on this issue. I guess I interpreted that to mean more meaningful/fundamental improvements, since the forking problem is a much larger issue, well beyond the scope of this here..
If I came of as rude, I'm sorry about that. It was never my intention. I was merely trying to express my technical opinion of your code. Maybe I was too blunt - might be my inherent german-ness (germanity?) or my mastery of the english language. Anyway, I appreciate your willingness to help here, I just don't agree with your proposal.
I only did any of this in light of the fact that you specifically asked for "progress" on this issue. I guess I interpreted that to mean more meaningful/fundamental improvements, since the forking problem is a much larger issue, well beyond the scope of this here..
I was more asking about @zanchey's topic branch and the work on the buffering issue. The buffering also bites us in other respects - look for bugs about functions running in the background, so it should be fixed anyway, which would also fix psub (well, that and the missing "$").
In that light, your ramdisk idea comes across as optimization work, and for that I didn't like the added complexity - the added forks (via e.g. math) might also cost more performance than they save, especially in short-lived psubs.
I'm removing the "next-2.x" label because this has been open for three years. There is no reason to think this will become a priority to fix anytime soon.
Hello there, I have encountered a situation where fish hangs upon process substitution, like in paste (tail -f foo | psub) (tail -f bar | psub). Bash analogue works fine. Is this related to this issue? And does some workaround exist?
@urxvtcd: Yes, that's this issue. psub currently only returns when the file has been fully written, and with tail -f that never happens since it keeps writing.
The workaround is to use something that can follow multiple files at the same time (like multitail). Or use multiple terminal windows with one tail each, or use e.g. tmux.
Alternatively, and I recommend against this, you can make your own fifos (with mkfifo) and then redirect the tail outputs into those.
Note that, if you background the tails, this will pretty much by necessity create dangling jobs - tail -f won't ever quit, so whatever is reading it won't either. I expect this to be an issue with bash as well.
While fixing #4222 I updated the psub documentation to clarify that --file is the default behavior. I also added a new --fifo flag to request the use of a named pipe and documented when and why you shouldn't use that flag. We still need to find a way to make using --fifo safe from deadlock but that's going to require fundamental changes to fish internals.
It seems to me that there is a fundamental semantic difference between pipe-based process substitution (ie. psub --fifo<(..)) and command substitution: Command substitution must wait for the enclosed pipeline/job/whatever to complete (It has to gather all the output), while pipe-based process substitution must not due to the nature of a pipe (Whether it is named or an fd is irrelevant). I believe that this is why it is so hard to get psub --fifo to work beyond a single pipe buffer of output. No amount of backgrounding or changing utilities in psub will ever get the command substitution to let go of its enclosed job, before it is done. By pure coincidence this actually does manage to work:
echo (cat'some_"large"_file'|psub--fifo&)
That looks very scary (to me, at least), but I think #238 might actually cause this to work consistently for me so far. Another thing that works is the following (This example is contrived and admittedly completely ridiculous):
echo (cat'some_"large"_file'|psub--fifo|xargscat)
Oof that hurts, but I think this illustrates quite clearly that the problem is not with the pipe, nor with psub, but with command substitution. While it may seem similar to pipe-based process substitution on the surface, their semantics are different. So different that trying to combine them together may be hard if not impossible (or a really really dirty hack).
psub --file (and zsh =(...)) on the other hand is semantically similar to command substitution. It will always faithfully capture all its input into its file (ie. wait for the preceding process to finish) and dutifully return the filename afterwards. By that point the earlier parts of the pipeline will also be finished so the command substitution can return. psub --file is also safer to use since it returns true files.
Given this I would prefer to have a separate construct for pipe-based process substitution, so that I am aware that what I am about to do will spawn a background job, that it will be taken care of for me by fish and that what I get back will be a (named) pipe file name that will also be taken care of for me by fish. And also that I will be properly aware of all the gotchas (Of which I am sure there are plenty). I love fish for its adherence to a simple clean syntax, but if a construct has complex semantics like pipe-based process substitution, it had better stand out. This is clearly not the case right now, given how common command substitution is.
Reacted by Beni Cherniavsky-Paskin, Thayne McCombs, Autumn and Andreas Källberg
cannot work unless fish is modified so as not to wait for cmd1 in cmd1 | cmd2 any longer (like in the Bourne shell or AT&T ksh).
The (...) will not be substituted until producer | psub -F exits and that's not before producer exits. psub ultimately runs a tee some-fifo. tee will hand on the open(O_WRONLY) on that fifo until somebody (typically the consumer) opens it in read mode. That is not going to happen before (...) expands. So you'll get a deadlock unless consumer manages to write all its output in the pipe buffer and exits.
The only way to fix is to not wait for the producer. If there's a fifo, it's meant to be an IPC system, with commands running concurrently, not one after the other.
Another problem with the current approach is that it creates an extra process (cat or tee) that shoves data around for no good reason (instead of the producer writing directly to the pipe)
The interface would probably have to change, like in:
diff -u (psub cmd1) (psub cmd2)
With which you could do away with named pipe and use the far cleaner /dev/fd/x.
That psub would create a pipe, output /dev/fd/x (where x is the reading end of the pipe) for the command substitution and run cmd1 in background with its output going to the writing end of the pipe.
And you could have a psub -w to do the same as ksh's >(...).
You'd still need to implement a pipe builtin or operator though (like yash's x>|y). IMO, it would still be better to just implement <(...), >(...) properly a la ksh.
2. What is 'process substitution'?
Process substitution (which itself is something of a misnomer) was a concept introduced in the Korn shell (ksh88)
ksh had process substitution already before ksh86. ksh86 is when it was first documented. In that version, the reading ones <(...) could actually be written (...) like fish's command substitution as in:
* _Aside from the fact that bash is the only shell which can fall back to using a FIFO (on systems which lack numbered file descriptors), all other shells which implement process substitution use numbered file descriptors, not named pipes.
That's not true, when zsh and Byron Rakitzis's rc introduced theirs (in 1.0 in 1990 for zsh, 0.9 in 1991 for rc, a few years before bash, but after ksh's and Tom Duff's original rc), they were using named pipes only.
It's only later that they switched to unnamed pipes and /dev/fd on systems that supported them (but still fell back to named pipes on others). That was in 2.6beta17 (1996) for zsh and 1.2 for rc (later in 1991)
The ability to use named pipes when /dev/fd is not available was added to ksh93 in ksh93u+ in 2011.
(in any case, that's no about numbered file descriptors but about being able to refer to file descriptors with /dev/fd/x).
@stephane-chazelas Thanks for that, I was finding that things like cat (yes | psub) were blocking, and your post perfectly explains why it does so. Rewriting the psub function so that the syntax is cat (psub yes) by changing the line to $argv | command tee $filename >/dev/null & seems to fix it. The downside of this though is that if you do it this way there doesn't seem to be a clean way to support pipe constructions within there likecat <(yes | grep -v "n").
You could do cat (psub "yes | grep -v 'n'") and use eval $argv in the script, but that seems to deadlock, probably related to fish not supporting concurrent execution. Doing an explicit fork (fish -c "$argv ...) seems to work, but overall the syntax is not really convenient.
Edit: Although I guess it's similar to the syntax for moreutils pee (which implements input process substitution)
Add dedicated syntax for process substitution, so that the shell can handle passing the /dev/fd or fifo file to the process, and run the substituted pipeline in the background. I.e. Do what bash and zsh do. It sounds like there is opposition to that though.
Add special magic for the psub command, so that if a command substitution ends with a psub command, it will run the pipeline in the background. So basically, using psub is the special syntax for process substitutuion. I don't love the magic, but there is some precedent for it, like how string split behaves specially if it is at the end of a command substitution, and even the current psub will throw an error if it isn't used inside of a command substitution.
Bless the syntax (somecommand | psub --fifo &). At first appearance, it seems like this would have a race condition, where the substitution might happen before psub outputs the file name. But it seems like the way it actually works is it waits for stdout of the subcommand to complete, so maybe it just works fine?
Use an external command that basically does the equivalent of the following pseudocode:
pipe = mkfifo() // or use /dev/fd
if fork() {
exec(argv, stdin=pipe)
} else {
print(pipe.filename)
}
But this has the downside that you have to pass the command to run to this tool, which means it probably either needs to run fish to interpret the command, or be the fish executable itself.
I don't really love any of these though.
However, I think most of these could also offer a path forward for #1786 as well.
Just have similar syntax for output
psub, or a new command could have special behavior if it is at the beginning of a command substitution, although, that's a little weird, because psub needs to basically write to it's own stdout and the original stdout of the pipeline.
This one is tricky. One possibility could be (psub --fifo | cmd >/dev/null 2>&1 &) and have psub write the filename to stderr instead of stdout. But that is kind of gross.
Same command could have an option to forward stdout from the pipeline instead of stdin into it.
Reacted by Patrick Häcker, Claudio Yanes, Zorbatron and Sebastian Noack
Activity
ridiculousfish commentedon Oct 14, 2013
Nice diagnosis and test case!
use mktemp(1) to generate temporary file names
use mktemp(1) to generate temporary file names
use mktemp(1) to generate temporary file names
psub: fix to enable fifo usage
zanchey commentedon Sep 30, 2014
I've pushed a fix to a topic branch (19217f3 on psub_fix), but I don't want to merge to master until the buffering issue is worked out.
Revert "Fix missing variable expansion $ in psub"
faho commentedon Aug 31, 2015
Any progress on this?
Revert "Fix missing variable expansion $ in psub"
geoff-nixon commentedon Sep 24, 2015
@faho Yes. And no. But, yes.
But before we get to the good stuff, lets review:
psubman page.Virtually unchanged from when it was written 10 years ago for fish 1.0.0, literally (and by literally, I mean literally) every sentence in
man psubis wrong (except for the example). With that out of the way...Process substitution (which itself is something of a misnomer) was a concept introduced in the Korn shell (ksh88) as a shorthand for the often messy process of using file descriptors and named pipes with a command or program when the program expects a file as an argument. In other words, it is a way to emulate including these programs in a UNIX pipeline, where one otherwise could not, either simply by design (
man dd), or for some other clever reason (man tee). The syntax used by shells that support this is:The standard output of
processis fed into a file descriptor or named pipe, which is passed as an argument tocommand.and
process >(command) ...The standard input of
commandis read from whateverprocessis doing with that argument (ideally, producing output).This syntax is by no means standard, and it certainly is not POSIX. It is supported by
ksh88andksh93, but notpdkshormksh;bashsupports it, but not inshmode (bash --posix). It is supported inzshwhen not in an emulation mode that proscribes it; andzshalso has another syntax,=(process), which we'll get to in a bit.The
fishpsubfunction:(purportedly) capitalizes on the fact that no special syntax is needed to perform "process substitution"; the ordinary syntax of command substitution (
commandor$(command)in Bourne shells,(command)is fish) can be used to accomplish the same thing pipeline within the command substitution whose standard output culminates in a file descriptor or named pipe.So, why has this bug stayed in place since it was introduced seven years ago (c6ebb23), two years since I opened this issue, followed by about a dozen others?
This is where it gets tricky, and there are two factors at play.
a. Many utilities which take files as arguments (i.e., for input or output), do so for a reason. A buffered UNIX pipeline often simply is not acceptable ... blah blah blah I've been up all night hacking on this so I'll finish my blustering treatise later. 💀
Basically, I propose we use a ramdisk. It combines to the "durability", ability to handle large files and non-streamable data of "file substitution" (?) (zsh
=(command), what we're doing withpsubat present) with the speed and ephemeral nature of using a fd or fifo.@ridiculousfish @zanchey @anyone @anyone @bueller... Initial thoughts?
faho commentedon Sep 24, 2015
I don't think so - it could be improved but I don't see much that is wrong. A bit imprecise and awkward maybe but assuming psub were bug-free it would mostly be sort-of correct. Anyway, this isn't really important to the matter at hand - we should improve psub documentation, but mostly we should just improve psub.
Or try to
commordiffthe output of two commands - how would you specify that? Piping syntax doesn't scale beyond a one-to-one relationship (at least I haven't ever seen how that'd work).Nope - "mount: only root can use "--types" option". (There is probably a way, but that one's not it)
This doesn't seem great - you're creating a ramdisk with one tool and then only detach it if another tool exists?
Anyway, I don't see the merits of this approach for my system - /tmp (where current psub stores its fifo or file) is already a tmpfs. As my cursory googling shows, it's the default on archlinux (my distro), Fedora,
Debian(nope, they reverted) and maybe Ubuntu, so other linuxen also won't benefit from this - at all. Even for those that don't have /tmp on tmpfs, we could use /run instead, which is explicitly defined to be one.Plus, fifos have an advantage that you don't have here - they can be filled in the background, which means the reading side can start earlier (which is presumably why zsh offers both fifos and files). Check the current psub source (try not to step on the bugs) - the fifo path does so, while the file path does not. Also, this increases the code complexity, especially the amount of OS-specific code.
Can't say I'm a fan.
geoff-nixon commentedon Sep 24, 2015
It is important if you one wants to know what we're trying to accomplish with this. Line-by-line:
psubandpsub -fhave identical behavior. Its also not necessarily true in any case, depending on how the stream is buffered, disk throughput, etc.Every sentence.
On to more relevant matters:
You're quoting right where I dropped off there, so I'm not sure we actually disagree here.
But my point here is threefold:
No, while I probably don't need this guard line any more as I've since wrapped it in an
ifblock, what I'm doing is handling an annoying fish "feature" which does not allow you to squash the output of attempting to use a command that does not exist.Perhaps you missed my joke:
That is to say, it's likely already implemented. As in, when your init scripts ran
mount -t tmpfsas root.There's nothing preventing one from reading a regular file while its still being written.
tail -f?The difference is a fifo is buffered, which is also why so many programs fail with fifos.
There is ample precedent for this. There is an immense ammount of OS-specific code in fish.
ls.fish.open.fish. How man__fish_systemctl_SOMETHING.fishfunctions are there?Well... sorry, I guess? We can't all run Arch Linux.
And I must say, pretty rude IMO, considering I only did any of this in light of the fact that you specifically asked for "progress" on this issue. I guess I interpreted that to mean more meaningful/fundamental improvements, since the forking problem is a much larger issue, well beyond the scope of this here..
If all you're looking for is a once-off workaround for 'THIS FISH DON'T FORK!', and you just want
psubto work just like the<(kshisms), all you basically need is to fork the background process yourself.This patch should do it.
faho commentedon Sep 24, 2015
Granted, but minor.
Sure that zsh does it that way?
"assuming psub were bug-free".
We don't - I was expanding on your point.
Ah okay. In that case, shouldn't you do that with anything? Or isn't the error output here kinda important? You're leaking tmpfss (if I understand correctly).
I was trying to say that it might be more complicated for other systems, though now I see that we probably could use /run on linux and just do the ramdisk setup on OSX/BSD.
Maybe we should consider running the
cat > filein the background then, too?This seems a bit more complicated than most OS-specific paths.
For the record, I'm a bit annoyed by those, mostly since most of them are only used by the
systemctlcompletion, AFAIK (I've thought about moving them into that, but I wanted to look into why they were moved out). Also, this is in completions, which are much less critical than psub.If I came of as rude, I'm sorry about that. It was never my intention. I was merely trying to express my technical opinion of your code. Maybe I was too blunt - might be my inherent german-ness (germanity?) or my mastery of the english language. Anyway, I appreciate your willingness to help here, I just don't agree with your proposal.
I was more asking about @zanchey's topic branch and the work on the buffering issue. The buffering also bites us in other respects - look for bugs about functions running in the background, so it should be fixed anyway, which would also fix psub (well, that and the missing "$").
In that light, your ramdisk idea comes across as optimization work, and for that I didn't like the added complexity - the added forks (via e.g.
math) might also cost more performance than they save, especially in short-lived psubs.9 remaining items
krader1961 commentedon Sep 7, 2016
I'm removing the "next-2.x" label because this has been open for three years. There is no reason to think this will become a priority to fix anytime soon.
urxvtcd commentedon Apr 29, 2017
Hello there, I have encountered a situation where fish hangs upon process substitution, like in
paste (tail -f foo | psub) (tail -f bar | psub). Bash analogue works fine. Is this related to this issue? And does some workaround exist?faho commentedon Apr 29, 2017
@urxvtcd: Yes, that's this issue.
psubcurrently only returns when the file has been fully written, and withtail -fthat never happens since it keeps writing.The workaround is to use something that can follow multiple files at the same time (like multitail). Or use multiple terminal windows with one tail each, or use e.g. tmux.
Alternatively, and I recommend against this, you can make your own fifos (with
mkfifo) and then redirect the tail outputs into those.Note that, if you background the
tails, this will pretty much by necessity create dangling jobs -tail -fwon't ever quit, so whatever is reading it won't either. I expect this to be an issue with bash as well.krader1961 commentedon Jul 17, 2017
While fixing #4222 I updated the
psubdocumentation to clarify that--fileis the default behavior. I also added a new--fifoflag to request the use of a named pipe and documented when and why you shouldn't use that flag. We still need to find a way to make using--fifosafe from deadlock but that's going to require fundamental changes to fish internals.use mktemp(1) to generate temporary file names
zanchey commentedon Apr 2, 2019
Did 6c22c88 fix this?
floam commentedon Apr 2, 2019
I don't think
catwould be any better.floam commentedon Apr 2, 2019
Whoops, I think the title of that commit message was reversed.
faho commentedon Apr 2, 2019
It did not -
echo (cat BIGFILE | psub --fifo)still doesn't complete, without "--fifo" it works.This sort of looks like fish is waiting for the
cat, i.e. the process writing to the psub?sniperrifle2004 commentedon Nov 8, 2019
It seems to me that there is a fundamental semantic difference between pipe-based process substitution (ie.
psub --fifo<(..)) and command substitution: Command substitution must wait for the enclosed pipeline/job/whatever to complete (It has to gather all the output), while pipe-based process substitution must not due to the nature of a pipe (Whether it is named or an fd is irrelevant). I believe that this is why it is so hard to getpsub --fifoto work beyond a single pipe buffer of output. No amount of backgrounding or changing utilities in psub will ever get the command substitution to let go of its enclosed job, before it is done. By pure coincidence this actually does manage to work:That looks very scary (to me, at least), but I think #238 might actually cause this to work consistently for me so far. Another thing that works is the following (This example is contrived and admittedly completely ridiculous):
Oof that hurts, but I think this illustrates quite clearly that the problem is not with the pipe, nor with psub, but with command substitution. While it may seem similar to pipe-based process substitution on the surface, their semantics are different. So different that trying to combine them together may be hard if not impossible (or a really really dirty hack).
psub --file(and zsh=(...)) on the other hand is semantically similar to command substitution. It will always faithfully capture all its input into its file (ie. wait for the preceding process to finish) and dutifully return the filename afterwards. By that point the earlier parts of the pipeline will also be finished so the command substitution can return.psub --fileis also safer to use since it returns true files.Given this I would prefer to have a separate construct for pipe-based process substitution, so that I am aware that what I am about to do will spawn a background job, that it will be taken care of for me by fish and that what I get back will be a (named) pipe file name that will also be taken care of for me by fish. And also that I will be properly aware of all the gotchas (Of which I am sure there are plenty). I love fish for its adherence to a simple clean syntax, but if a construct has complex semantics like pipe-based process substitution, it had better stand out. This is clearly not the case right now, given how common command substitution is.
stephane-chazelas commentedon Nov 10, 2019
In case nobody has made that case yet
cannot work unless
fishis modified so as not to wait forcmd1incmd1 | cmd2any longer (like in the Bourne shell or AT&T ksh).The
(...)will not be substituted untilproducer | psub -Fexits and that's not beforeproducerexits.psubultimately runs atee some-fifo.teewill hand on theopen(O_WRONLY)on that fifo until somebody (typically theconsumer) opens it in read mode. That is not going to happen before(...)expands. So you'll get a deadlock unlessconsumermanages to write all its output in the pipe buffer and exits.The only way to fix is to not wait for the producer. If there's a fifo, it's meant to be an IPC system, with commands running concurrently, not one after the other.
Another problem with the current approach is that it creates an extra process (
catortee) that shoves data around for no good reason (instead of theproducerwriting directly to the pipe)The interface would probably have to change, like in:
With which you could do away with named pipe and use the far cleaner
/dev/fd/x.That
psubwould create a pipe, output/dev/fd/x(where x is the reading end of the pipe) for the command substitution and runcmd1in background with its output going to the writing end of the pipe.And you could have a
psub -wto do the same as ksh's>(...).You'd still need to implement a
pipebuiltin or operator though (likeyash'sx>|y). IMO, it would still be better to just implement<(...),>(...)properly a la ksh.stephane-chazelas commentedon Nov 10, 2019
ksh had process substitution already before ksh86. ksh86 is when it was first documented. In that version, the reading ones
<(...)could actually be written(...)likefish's command substitution as in:I don't know when that one was removed.
stephane-chazelas commentedon Nov 10, 2019
That's not true, when zsh and Byron Rakitzis's rc introduced theirs (in 1.0 in 1990 for zsh, 0.9 in 1991 for rc, a few years before bash, but after ksh's and Tom Duff's original rc), they were using named pipes only.
It's only later that they switched to unnamed pipes and /dev/fd on systems that supported them (but still fell back to named pipes on others). That was in 2.6beta17 (1996) for zsh and 1.2 for rc (later in 1991)
The ability to use named pipes when /dev/fd is not available was added to ksh93 in ksh93u+ in 2011.
(in any case, that's no about numbered file descriptors but about being able to refer to file descriptors with /dev/fd/x).
krackers commentedon Aug 27, 2023
@stephane-chazelas Thanks for that, I was finding that things like
cat (yes | psub)were blocking, and your post perfectly explains why it does so. Rewriting the psub function so that the syntax iscat (psub yes)by changing the line to$argv | command tee $filename >/dev/null &seems to fix it. The downside of this though is that if you do it this way there doesn't seem to be a clean way to support pipe constructions within there likecat <(yes | grep -v "n").You could do
cat (psub "yes | grep -v 'n'")and useeval $argvin the script, but that seems to deadlock, probably related to fish not supporting concurrent execution. Doing an explicit fork (fish -c "$argv ...) seems to work, but overall the syntax is not really convenient.Edit: Although I guess it's similar to the syntax for moreutils pee (which implements input process substitution)
tmccombs commentedon Jan 7, 2025
I see a few paths forward for this:
psubis the special syntax for process substitutuion. I don't love the magic, but there is some precedent for it, like howstring splitbehaves specially if it is at the end of a command substitution, and even the currentpsubwill throw an error if it isn't used inside of a command substitution.(somecommand | psub --fifo &). At first appearance, it seems like this would have a race condition, where the substitution might happen before psub outputs the file name. But it seems like the way it actually works is it waits for stdout of the subcommand to complete, so maybe it just works fine?fishto interpret the command, or be thefishexecutable itself.I don't really love any of these though.
However, I think most of these could also offer a path forward for #1786 as well.
psubneeds to basically write to it's own stdout and the original stdout of the pipeline.(psub --fifo | cmd >/dev/null 2>&1 &)and havepsubwrite the filename to stderr instead of stdout. But that is kind of gross.