Skip to content

Don't start keepalive process if the first or last process is internal - #4653

Closed
faho wants to merge 1 commit into
fish-shell:masterfrom
faho:less-keepalive
Closed

faho wants to merge 1 commit into
fish-shell:masterfrom
faho:less-keepalive

Conversation

@faho

@faho faho commented Jan 10, 2018 •

Copy link
Copy Markdown
Member

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:

  • [N/A] Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.md

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.
@faho faho added the bug Something that's not working as intended label Jan 10, 2018
@faho faho added this to the fish-3.0 milestone Jan 10, 2018
@faho

faho commented Jan 10, 2018

Copy link
Copy Markdown
Member Author

Also, #1700 is the same thing as #4540.

@faho

faho commented Jan 10, 2018

Copy link
Copy Markdown
Member Author

Okay, so the condition isn't quite correct here - e.g. sudo echo lol | grep lol (with grep being a wrapper function around the command), this would not use a keepalive process, but instead join fish's pgid, even though fish isn't involved in reading/writing at all.

The thing is though that the keepalive process isn't necessary at all. See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html:

4.14 Process ID Reuse

A process group ID shall not be reused by the system until the process group lifetime ends.

A process ID shall not be reused by the system until the process lifetime ends. In addition, if there exists a process group whose process group ID is equal to that process ID, the process ID shall not be reused by the system until the process group lifetime ends. A process that is not a system process shall not have a process ID of 1.


I still believe that:

  • Removing the keepalive process is correct

  • Having the process remain in fish's pgid (which means not needing to call tcsetpgrp() at all, which means it is possible to use posix_spawn) is also correct IFF fish is involved in reading/writing (because a builtin is connected to the terminal)

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 read | cat case, and that's kinda important.

@zanchey

zanchey commented Jan 20, 2018

Copy link
Copy Markdown
Member

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 command1 | function | command2 case, in order to ensure that command1 and command2 end up in the same process group?

@faho

faho commented Jan 20, 2018

Copy link
Copy Markdown
Member Author

Don't you need the keepalive in the command1 | function | command2 case, in order to ensure that command1 and command2 end up in the same process group?

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.

@ridiculousfish

Copy link
Copy Markdown
Member

In the case of command1 | function | command2 it is true that command1 may finish and exit before command2 starts. In principle this is fine: command2 may join command1's pgroup even if command1 has exited, as long as it has not yet been reaped.

Unfortunately fish doesn't know how to delay reaping the first process. function may spawn processes, fish will waitpid(-1) for them, and so the first process may be prematurely reaped. So the keepalive isn't so much about pid recycling as it is just about making sure every command in the job ends up in the same process group.

@faho I'm unsure about whether it's right to check the pipeline position. For example:

cat file.txt | begin
    read | cat
end

Here read is first in the pipeline, but its input is not from the terminal. (The redirection is provided in the block_io part of the job). Do you have thoughts on that case?

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).

@faho

faho commented Feb 2, 2018

Copy link
Copy Markdown
Member Author

So the keepalive isn't so much about pid recycling as it is just about making sure every command in the job ends up in the same process group.

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.

Unfortunately fish doesn't know how to delay reaping the first process.

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 😄.

Here read is first in the pipeline, but its input is not from the terminal.

This is a more general thing we need to fix - the read | cat is started as its own job that has no connection to the rest of the pipeline (which is connected to a variety of bugs - see #4238). So before we actually do that, the job looks like cat | block-or-function.

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 cat's stdin is still connected to the terminal, that needs to stay in the foreground as well (IIUC we can't switch foreground pgroups midway and then SIGCONT the cat if it stops). So the inner job needs to be in cat's pgroup as well, so if the inner job is fish, cat needs to be in fish's pgroup. Which means the entire function needs to be in fish's pgroup.

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?

@ridiculousfish

Copy link
Copy Markdown
Member

Would it be possible to start reaping only once the entire job has ended?

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 waitpid(-1...), and only waiting on specific process groups waitpid(-pgid...). We would then need a solution for reaping background jobs, and also the wait builtin.

My gut says that this is more complex than the existing keepalive scheme, which avoids reaping the first process by just keeping it alive.

Anyway, all this doesn't matter if fish is the process group leader like in this PR

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.

@faho

faho commented Feb 4, 2018

Copy link
Copy Markdown
Member Author

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.

Currently, those are separate jobs (if I'm reading all this correctly). So we could await once the entire job has been constructed.

only waiting on specific process groups waitpid(-pgid...)

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 wait on the PIDs in turn.

We would then need a solution for reaping background jobs, and also the wait builtin.

Isn't it the same principle for these? Only wait once the job has been fully constructed.

After pondering this I wonder if it makes sense for fish to simply grab control of the terminal when executing a builtin.

And what about the cat part of read | cat then? Wouldn't that then get SIGTTOUd?

@mqudsi

mqudsi commented Mar 4, 2018

Copy link
Copy Markdown
Contributor

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 IO_BUFFER nonsense, which wouldn't be needed if we had a proper DAG to keep track of which job relies on which (nested) job and only wait the top-level jobs in such a case: all processes would be started asynchronously solving the need to buffer anything, blocking until the pipe is written to or closed.

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 exec_job function while manipulating virtually global state (the io_chain). Like @faho notes, there's simply no relationship between the jobs in a single pipeline (just a chain of pipes and buffers).

The other problem is that while the job_t structure is used for job control within exec/proc.cpp, outside of those the tokenizer tokens themselves with the grammar attached seem to be used (such as the list of jobs), meaning changes there have to propagate backwards all the way to the parser and the tokenizer. There's virtually no proper abstraction between the layers, it ends up being a compromise between either using hacks (such as static global pointers to transient entities :shudder:) to avoid making huge changes or rewriting huge chunks just to properly pass context between the layers.

@ridiculousfish

Copy link
Copy Markdown
Member

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 cd builtin. If two functions executing concurrently both invoke cd, then they will race, and one will probably misbehave; fixing this requires instancing essentially the entire process environment (or, you know, forking). So it would be a very large effort.

@faho

faho commented May 5, 2018

Copy link
Copy Markdown
Member Author

I'm withdrawing this because we need something better.

@faho faho closed this May 5, 2018
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
@faho
faho deleted the less-keepalive branch January 17, 2024 09:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something that's not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read | cat crashes fish

4 participants