Repository navigation
Conversation
Currently, fish spawns a keepalive process when a non-external process (a block/builtin/function) is inside a pipeline. This keepalive process is then made the process group leader, which theoretically stops the process group from being coopted by PID reuse (which I'm still not sure can actually happen). But if a non-external process is first or last in the pipeline, then fish itself needs to read/write to the terminal, so it should remain pgroup leader, and therefore the keepalive process is not needed. Fixes fish-shell#4540.
|
Okay, so the condition isn't quite correct here - e.g. The thing is though that the keepalive process isn't necessary at all. See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html:
I still believe that:
But to get that latter part correct, we need to figure that last condition out - which needs to involve diving down into the functions to figure out if a builtin is connected to the terminal. I do believe this PR is an improvement - it fixes the |
|
I've merged this into my working version and will test it over the next few days. Don't you need the keepalive in the |
Well... it's one way to do it (and this currently still uses a keepalive in that case), but it's a rather roundabout way of achieving it. IIUC, the "proper" way to do it is to use the first started command as the pgroup leader, and make all other commands (even the ones started via function) join that. I.e. the function of the keepalive process would be taken over by the first started command. This really is just about the case where fish needs to stay in the foreground, which makes using the keepalive pgroup wrong. |
|
In the case of Unfortunately fish doesn't know how to delay reaping the first process. @faho I'm unsure about whether it's right to check the pipeline position. For example: Here Also I think the WSL SIGCONT stuff may mask issues since it can artificially extend the process lifetime; let's not merge this until #4676 is merged or rejected (I'll try to get to that today). |
So you mean that, after every other command in the job has already exited, you start a new one and it doesn't get the same pgroup id as all the other commands? I can kind of see that, yes.
If I'm reading this correctly, the first process isn't really special in this case. The process group stays alive as long as there's any process left in it, so I would assume it can still be joined. But I'm assuming if fish doesn't know to wait before reaping the first process in a pgroup, it also doesn't know to wait before reaping the last? Would it be possible to start reaping only once the entire job has ended? Anyway, all this doesn't matter if fish is the process group leader like in this PR, but I'm sure you know that 😄.
This is a more general thing we need to fix - the Now, that block-or-function may contain a variety of jobs, and for each of those if a builtin is connected to the block-or-function's stdout (since it't not redirected), that needs to be in fish's pgroup. And since the outer So we need to either drill down into the function and figure out if a builtin is connected to stdout anywhere, or we just keep the entire thing in fish's pgroup. The former of which seems rather complicated, and I'm not sure if it's worth it. IIUC, the difference is basically in SIGINT-handling - the foreground pgroup receives it, so if fish stays in the foreground, then fish gets it as well. Which for that block above might actually be what you want (to kill the entire block with one ctrl-c). Either that, or we introduce subshells (which brings with it other issues). Does that make any sort of sense? |
If we could avoid reaping the first process until the entire job is constructed, that would indeed neatly our problem. The tricky part is the pipeline may have a function which in turn invokes external commands, which of course must complete before the function returns, and so must be awaited. So we need to reap some processes, just one that one process. It's possible we could achieve this by eliminating all uses of My gut says that this is more complex than the existing keepalive scheme, which avoids reaping the first process by just keeping it alive.
Yep. The original issue (the crash) only affects built-ins, right? After pondering this I wonder if it makes sense for fish to simply grab control of the terminal when executing a builtin. That seems like a less invasive change than running external commands in fish's pgroup, which would probably have implications for backgrounding, etc. |
Currently, those are separate jobs (if I'm reading all this correctly). So we could await once the entire job has been constructed.
That wouldn't work since these "subjobs" need to have the same pgid as the "parent job" (which they currently don't, but should - that's #4238). We'd have to
Isn't it the same principle for these? Only
And what about the |
|
fwiw I've been working on this general issue for a while now (it would have been helpful to read this thread before starting, it was during the time github wasn't sending me notifications properly for existing issues/prs) but that's some really ugly code I'm having to deal with. The real motivation wasn't getting rid of the keepalive process so much as doing away with the The problem is that the definition of "job" is so convoluted; I hadn't realized how many "jobs" a single statement could trigger, and there is no state maintained between them, they simply recurse into the The other problem is that while the |
|
fish has deeply in-built notions of things like "the currently executing function" that will be very difficult to uproot. As a simple example, consider the |
|
I'm withdrawing this because we need something better. |
Description
Currently, fish spawns a keepalive process when a non-external
process (a block/builtin/function) is inside a pipeline.
This keepalive process is then made the process group leader, which
theoretically stops the process group from being coopted by PID
reuse (which I'm still not sure can actually happen).
But if a non-external process is first or last in the pipeline, then
fish itself needs to read/write to the terminal, so it should remain
pgroup leader, and therefore the keepalive process is not needed.
Fixes #4540.
TODOs: