Repository navigation
Description
When executing a pipeline with read at the end, it does an interactive read, but the first typed character causes the read to bail. It turns out what's going on is that read waits on select() just fine, but gets EIO when it finally calls read() on stdin. The EIO is because fish is no longer in the foreground process group; it spawned a keepalive process which took control of the terminal.
I don't know why the keepalive process is necessary. There's a comment saying this is so the process group doesn't die prematurely if the process group leader dies, but AFAIK POSIX requires that the process group keep living even if the process group leader dies, and even goes so far as to prevent new processes from reusing the PID of any dead process group leader as long as the process group is still alive. From what I've read, it sounds like the real issue is if the session leader dies, but that's not something that pipelines have to worry about.
Of course, without the keepalive, fish would still need a new process group for the pipeline, which would cause the same issue. Bash gets around this by actually forking and running the read in the child, but of course that's pretty useless as it can't affect the variables of its parent process.
We could change read to detect when it's part of a pipeline and disable interactive mode, though that seems kind of crappy. We could also just try and keep fish in the foreground process group when running the read builtin, since I believe fish waits until the builtin is complete before spawning any external commands anyway. Or maybe some other approach I haven't thought of would work.
Curiously, when running read a | cat and typing stuff, sometimes the calling fish process also exits, without error. It appears to believe the terminal closed, which is because the calling fish process actually got EIO. I'm not sure why this is; I'm assuming that somehow it's reading before its process group has been made the foreground process group again, but it doesn't make sense to me why this would be. I can't reproduce this issue with lldb attached, but adding in debug logging shows it getting EIO and exiting.
Activity
ridiculousfish commentedon Sep 22, 2014
It would be fantastic to be able to stop spawning the keepalive process. I wonder if it's needed for the case of
cmd1 | some_function | cmd2. Since some_function waits for all input to be received before proceeding, it's likely that cmd1 will exit before cmd2 can start, which means that the process group itself will exit. So cmd1 and cmd2 will not end up in the same process group. I'm not sure how bad this is.lilyball commentedon Sep 22, 2014
Hrm ok, I suppose that bit makes sense. Do builtins/functions (or rather, non-external commands) always run in isolation, without any other pipeline component running? That's my belief at the moment.
Given that, we could spawn the keepalive process if and only if there's a non-external anywhere in the pipeline other than at the head. If it's at the head, we won't have created a process group yet, so it doesn't matter.
And when we do spawn the keepalive, we should simply not make it the foreground process group. Once we spawn the next command in the pipeline, we can make it the foreground process group again, but when we're running a non-external thing, fish should be foreground. Theoretically that would actually let someone use
read foo </dev/ttyin the middle of a pipeline (i.e. in a function or block).ridiculousfish commentedon Sep 22, 2014
Yes, it's my understanding that builtins and functions run in isolation. That is, they wait for all input to be buffered, do their stuff (buffering all output), and then write all of the output, which itself requires another fork (the calls to exec_write_and_exit). Surely not optimal.
lilyball commentedon Sep 22, 2014
Yeah, definitely not optimal, but as long as functions/builtins/etc operate on string buffers I'm not sure it can really change.
I am very curious about the fork-and-print-buffer scheme though. Why doesn't fish just do that in a thread?
ridiculousfish commentedon Sep 22, 2014
I think it should use a thread, that's been a fix I've been planning for a while.
mqudsi commentedon Sep 30, 2018
This is fixed in fish 3.0 (does not reproduce under
master).