Skip to content

Fixes a race condition in output chaining in job control - #4246

Closed
mqudsi wants to merge 11 commits into
fish-shell:masterfrom
mqudsi:jobcontrol_race_condition_fix
Closed

mqudsi wants to merge 11 commits into
fish-shell:masterfrom
mqudsi:jobcontrol_race_condition_fix

Conversation

@mqudsi

@mqudsi mqudsi commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

I'm not sure if this happens on all platforms, but under WSL with the
existing codebase, processes in the job chain that pipe their
stdout/stderr to the next process in the job could terminate before the
next job started (on fast enough machines for quick enough jobs).

This caused issues like #4235 and possibly #3952, at least for external
commands. What was happening is that the first process was finishing
before the second process was fully set up. fish would then try to
assign (in both the child and the parent) the process group id belonging
to the process group leader to the new process; and if the first process
had already terminated, it would have ended its process group with it as
well before that happened.

I'm not sure if there was already a mechanism in place for ensuring that
a process remains running at least as long as it takes for the next
process in the chain to join its group, etc., but if that code was
there, it wasn't working in my test setup (WSL).

(Actually, I'm sure there is because this would have been a much bigger
deal for everyone if there was no such mechanism.)

I'm not sure how I should
handle non-external commands (and external commands executed via
posix_spawn). I don't know if they are affected by the race condition in
the first place, but when I tried to add the same "wait for next command
in chain to run before unblocking" that would cause black screens
requiring ctrl+c to bypass.

I'm not sure if this happens on all platforms, but under WSL with the
existing codebase, processes in the job chain that pipe their
stdout/stderr to the next process in the job could terminate before the
next job started (on fast enough machines for quick enough jobs).

This caused issues like fish-shell#4235 and possibly fish-shell#3952, at least for external
commands. What was happening is that the first process was finishing
before the second process was fully set up. fish would then try to
assign (in both the child and the parent) the process group id belonging
to the process group leader to the new process; and if the first process
had already terminated, it would have ended its process group with it as
well before that happened.

I'm not sure if there was already a mechanism in place for ensuring that
a process remains running at least as long as it takes for the next
process in the chain to join its group, etc., but if that code was
there, it wasn't working in my test setup (WSL).

This patch definitely needs some review; I'm not sure how I should
handle non-external commands (and external commands executed via
posix_spawn). I don't know if they are affected by the race condition in
the first place, but when I tried to add the same "wait for next command
in chain to run before unblocking" that would cause black screens
requiring ctrl+c to bypass.

The "unblock previous command" code was originally run by the next child
to be forked, but was then moved to the shell code instead, making it
more-centrally located and less error-prone.

Note that additional headers may be required for the mmap system call on
other platforms.
@mqudsi mqudsi changed the title Fixes a race condition in output redirection in job chain Fixes a race condition in output chaining in job control Jul 25, 2017
mqudsi added 2 commits July 25, 2017 19:19
This code should be more portable, and certainly cleaner. We are
currently always sending SIGCONT to the last process (if it was part of
a job chain) regardless of whether it called SIGSTOP on itself or not,
which should be fine.

Need to explore whether or not the other forks in src/exec.cpp need to
be SIGSTOP'd on run or only the one that we included in this patch.
@mqudsi

mqudsi commented Jul 26, 2017

Copy link
Copy Markdown
Contributor Author

I've studied the code more and see that needs_keepalive and keepalive.pid were used to work around the process group dying. This was used exclusively for builtins/functions (p->type != EXTERNAL), which answers my earlier question about whether or not builtins suffered from the same problem.

I hadn't realized that the for loop is called multiple times for a given
"single input" (anything that doesn't include semicolons, etc) to fish,
and so processes were being blocked but blocked_pid was lost by the time
that the next job (which was reading from the last process in the
previous job) came around.

Now using a static variable to store the last blocked PID. AFAICT, this
main job control loop is always executed from the same process and
thread, so this shouldn't need to be wrapped in atomics/mutexes, etc.
@mqudsi
mqudsi force-pushed the jobcontrol_race_condition_fix branch from 12d2946 to c36e17a Compare July 26, 2017 03:58
@krader1961

Copy link
Copy Markdown
Contributor

Are you running make test? Because most of your changes in this branch have failed on Travis. It's normal for a few failures to occur. Specifically with respect to the macOS environment since the Travis macOS sandbox is very flakey due to being oversubscribed. But in this case the failures have also been in the core Linux sandboxes where it is rare to see a failure that isn't due to problem with the change.

@mqudsi

mqudsi commented Jul 26, 2017

Copy link
Copy Markdown
Contributor Author

Yeah, I'm seeing the same failures locally as well. It doesn't make sense to me that the only failure is coming from history.expect, is there a way to keep the tests going after first failure to see if anything else pops up?

@krader1961

Copy link
Copy Markdown
Contributor

...is there a way to keep the tests going after first failure to see if anything else pops up?

Not that I am aware of. In my experience (1.7+ years of contributing) when the first unit test fails that is sufficient to tell me my change is wrong. Once in a great while I found an incorrect unit test as a result of some change I made that broke the unit tests.

@mqudsi

mqudsi commented Jul 26, 2017

Copy link
Copy Markdown
Contributor Author

No doubt. I was actually hoping to gain insight by seeing what else fails.

mqudsi added 3 commits July 26, 2017 14:19
We were having child processes SIGSTOP themselves immediately after
setting their process group and before launching their intended targets,
but they were not necessarily stopped by the time the next command was
being executed (so the opposite of the original race condition where
they might have finished executing by the time the next command came
around), and as a result when we sent them SIGCONT, that could never
reach. Now using waitpid to synchronize the SIGSTOP/SIGCONT between the
two.

If we had a good, unnamed inter-process event/semaphore, we could use
that to have a child process conditionally stop itself if the next
command in the job chain hadn't yet been started / setup, but this is
probably a lot more straightforward and less-confusing, which isn't a
bad thing.

Additionally, there was a bug caused by the fact that the main exec_job
loop actually blocks to read from previous commands in the job if the
current command is a built-in that doesn't need to fork.

With this waitpid code, I was able to finally add the SIGSTOP code to
all the fork'd processes in the main exec_job loop without introducing
deadlocks; it turns out that they should be treated just like the main
EXTERNAL fork, but they tend to execute faster causing the same deadlock
described above to occur more readily.

The only thing I'm not sure about is whether we should execute
unblock_pid undconditionally for all !EXTERNAL commands. It makes more
sense to *only* do that if a blocking read were about to be done in the
main loop, otherwise the original race condition could still appear
(though it is probably mitigated by whatever duration the SIGSTOP lasted
for, even if it is SIGCONT'd before the next command tries to join the
process group).
Setting the process group in a fork/exec scenario is a well-documented
race condition in pretty much any job control mechanism [0] [1]. The
Wikipedia article contradicts the glibc article and suggests that the
best approach is for the parent to wait for the child to become the
process group leader, while the glibc article suggests that both should
make it so (which is what fish did previously). However, I'm running
into cases where tcsetpgrp is causing an EPERM error, which it isn't
documented to do except if the session id for the calling process
differs from that of the target process group (which is never the case
in fish since they are all part of the same session), which should cause
a _different_ error (SIGTTOU to be sent to all members of the calling
process' group).

In all cases, this is easily remedied by checking if the process group
in question is already in control of the terimnal. There's still the
off-chance that in the time between we check that and the time that the
command completes that situation may have changed, but the parent
process is supposed to ignore the result of this call if it errors out.

[0]: https://en.wikipedia.org/wiki/Process_group
[1]: https://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html
…tions

In the last commit, we introduced an indiscriminate if !EXTERNAL check
that unblocks a previously SIGSTOP'd command (if any) to allow the main
loop in exec_job to read from it without deadlocking (since builtins and
functions read directly from input as an optimization, sometimes).

Now only unblocking where a fork will not happen to ensure that if a
builtin ends up forking, that fork'd process is guaranteed to be able to
join the previous process' process group and access its output pipes.
@mqudsi

mqudsi commented Jul 26, 2017

Copy link
Copy Markdown
Contributor Author

OK, I figured it out and the tests are passing (locally, at least) once more.

@krader1961

Copy link
Copy Markdown
Contributor

The most recent Travis run failed one of the Linux environments because it timed out waiting for more output. That suggests this introduces a new failure mode since I can't recall seeing that particular Travis failure in recent memory. In light of your other PR is this PR still relevant?

@krader1961

Copy link
Copy Markdown
Contributor

I restarted the failing test and it failed in the same manner:

Testing file random.in ... ok
Testing file read.in ... 
No output has been received in the last 10m0s, this potentially indicates a stalled build or something wrong with the build itself.
Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#Build-times-out-because-no-output-was-received
The build has been terminated

@mqudsi

mqudsi commented Aug 2, 2017 •

Copy link
Copy Markdown
Contributor Author

Yeah, I think we can close this. I had originally left this open as a separate PR in case there wouldn't be interest in a full overhaul of the exec_job code to at least work around the tcsetpgrp issue. I'm pretty sure there's a standalone commit in the the fork_exec_changes branch that fixes the waiting for stdin, but I can't be bothered to cherry-pick it since this PR is pointless now.

@mqudsi mqudsi closed this Aug 2, 2017
@mqudsi
mqudsi deleted the jobcontrol_race_condition_fix branch October 15, 2018 15:31
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants