From: Patrick Reynolds <hidden> Date: 2016-06-15 23:02:16
Blocked and ignored signals -- but not caught signals -- are inherited
across exec. Some callers with sloppy signal-handling behavior can call
git with SIGPIPE blocked or ignored, even non-deterministically. When
SIGPIPE is blocked or ignored, several git commands can run indefinitely,
ignoring EPIPE returns from write() calls, even when the process that
called them has gone away. Our specific case involved a pipe of git
diff-tree output to a script that reads a limited amount of diff data.
In an ideal world, git would never be called with SIGPIPE blocked or
ignored. But in the real world, several real potential callers, including
Perl, Apache, and Unicorn, sometimes spawn subprocesses with SIGPIPE
ignored. It is easier and more productive to harden git against this
mistake than to clean it up in every potential parent process.
Signed-off-by: Patrick Reynolds <redacted>
---
cache.h | 1 +
git.c | 5 +++++
setup.c | 11 +++++++++++
t/t0012-sigpipe.sh | 27 +++++++++++++++++++++++++++
4 files changed, 44 insertions(+)
create mode 100755 t/t0012-sigpipe.sh
@@ -0,0 +1,27 @@+#!/bin/sh++test_description='check handling of SIGPIPE'+../test-lib.sh++test_expect_success'create blob''+test-genrandomfoo16384>file&&+gitaddfile+'++large_git(){+foriin$(test_seq1100);do+gitdiff--staged--binary||return$?+done+}++test_expect_success'git dies with SIGPIPE''+OUT=$(((large_git;echo$?1>&3)|true)3>&1)+test"$OUT"-eq141+'++test_expect_success'git dies with SIGPIPE even if parent ignores it''+OUT=$(((trap""PIPE;large_git;echo$?1>&3)|true)3>&1)+test"$OUT"-eq141+'++test_done
From: Patrick Reynolds <hidden> Date: 2016-06-15 23:02:19
On Sun, Aug 17, 2014 at 8:14 PM, Eric Wong [off-list ref] wrote:
But unicorn would ignore SIGPIPE it if Ruby did not; relying on SIGPIPE
while doing any multiplexed I/O doesn't work well.
Exactly. Callers block SIGPIPE for their own legitimate reasons, but they
don't consistently unblock it before spawning a git subprocess that needs
the default SIGPIPE behavior. Easier to fix it in git than in every potential
caller.
--Patrick
From: Junio C Hamano <hidden> Date: 2016-06-15 23:02:32
Patrick Reynolds [off-list ref] writes:
Blocked and ignored signals -- but not caught signals -- are inherited
across exec. Some callers with sloppy signal-handling behavior can call
git with SIGPIPE blocked or ignored, even non-deterministically. When
SIGPIPE is blocked or ignored, several git commands can run indefinitely,
ignoring EPIPE returns from write() calls, even when the process that
called them has gone away. Our specific case involved a pipe of git
diff-tree output to a script that reads a limited amount of diff data.
In an ideal world, git would never be called with SIGPIPE blocked or
ignored. But in the real world, several real potential callers, including
Perl, Apache, and Unicorn, sometimes spawn subprocesses with SIGPIPE
ignored. It is easier and more productive to harden git against this
mistake than to clean it up in every potential parent process.
Signed-off-by: Patrick Reynolds <redacted>
I missed this one when it was posted.
I have a suspicion that $gmane/256544 (relevant parties Cc'ed) is
cured by this (even though the other topic came much later than this
change)?
@@ -865,3 +865,14 @@ int daemonize(void)return0;#endif}++/* un-ignore and un-block SIGPIPE */+voidsanitize_signals(void)+{+sigset_tunblock;++sigemptyset(&unblock);+sigaddset(&unblock,SIGPIPE);+sigprocmask(SIG_UNBLOCK,&unblock,NULL);+signal(SIGPIPE,SIG_DFL);
With the only caller in git.c, there is not a good reason that we
would want to have this as a global in a different file (I think the
patch merely follows the pattern of sanitize-fds, but that one has
to be called from many places).
After making the function static to git.c, it would also be a good
idea to rename it to match the comment at the sole callsite,
e.g. "restore_sigpipe_to_default()" or something. Then the comment
at the callsite can be removed.
Later somebody else may want to add other signals handled similarly
for whatever reason (I do not think of any signal or any good reason
at this moment). But they will have to come up with much better
justification than "the function name says 'sanitize'" if we named
it to something specific to SIGPIPE. And that good justification
will guide us what kind of signals need to be "sanitized" and find a
better verb than "sanitize", if it ever happens.
Hmmm, do we really need to allocate a new test number just for these
two tests, instead of folding it into an existing one?
+test_expect_success 'create blob' '
+ test-genrandom foo 16384 >file &&
+ git add file
+'
+
+large_git () {
+ for i in $(test_seq 1 100); do
+ git diff --staged --binary || return $?
Write it more like this:
for i in $(...)
do
git diff --cached --binary || return
done
to (1) avoid unnecessary semicolon before "do", (2) prefer the true
option name over a synonym, and (3) omit unnecessary $? that is
given to "return".
Summing them up, something like this squashed in would give us a
good end result, perhaps?
---
cache.h | 1 -
git.c | 25 +++++++++++++++++++++----
setup.c | 11 -----------
t/t0000-basic.sh | 17 +++++++++++++++++
t/t0012-sigpipe.sh | 27 ---------------------------
5 files changed, 38 insertions(+), 43 deletions(-)
delete mode 100755 t/t0012-sigpipe.sh
@@ -1063,4 +1063,21 @@ test_expect_success 'very long name in the index handled sanely' 'test$len=4098'+large_git(){+foriin$(test_seq1100)+do+gitls-files-s||return+done+}++test_expect_success'a constipated git dies with SIGPIPE''+OUT=$(((large_git;echo$?1>&3)|:)3>&1)+test"$OUT"-eq141+'++test_expect_success'a constipated git dies with SIGPIPE even if parent ignores it''+OUT=$(((trap""PIPE;large_git;echo$?1>&3)|:)3>&1)+test"$OUT"-eq141+'+ test_done
With the only caller in git.c, there is not a good reason that we
would want to have this as a global in a different file (I think the
patch merely follows the pattern of sanitize-fds, but that one has
to be called from many places).
Would we want to call it from external C commands, too? For the most
part, git.c is the entry point for running git commands, and any
sanitizing it does will be inherited by sub-commands. But it _is_ still
legal to call dashed commands individually, and even required in some
cases (e.g., git-upload-pack for ssh clients).
From: Patrick Reynolds <hidden> Date: 2016-06-15 23:02:33
On Wed, Sep 17, 2014 at 3:11 AM, Jeff King [off-list ref] wrote:
Would we want to call it from external C commands, too? For the most
part, git.c is the entry point for running git commands, and any
sanitizing it does will be inherited by sub-commands. But it _is_ still
legal to call dashed commands individually, and even required in some
cases (e.g., git-upload-pack for ssh clients).
git-upload-pack is protected pretty well from SIGPIPE shenanigans,
because its stdout all goes through write_or_die, as of cdf4fb8. We
did, long ago, have some EPIPE problems with upload-pack and SSH
clients, but it all predates cdf4fb8.
So I think it's redundant to unblock SIGPIPE in git-upload-pack.
I'll tidy up as Junio recommended, recheck the tests, and submit
an updated patch shortly.
--Patrick
From: Junio C Hamano <hidden> Date: 2016-06-15 23:02:33
Thanks; just to save time, you may want to look at what has already
been queued on 'pu'.
On Thu, Sep 18, 2014 at 7:35 AM, Patrick Reynolds [off-list ref] wrote:
On Wed, Sep 17, 2014 at 3:11 AM, Jeff King [off-list ref] wrote:
quoted
Would we want to call it from external C commands, too? For the most
part, git.c is the entry point for running git commands, and any
sanitizing it does will be inherited by sub-commands. But it _is_ still
legal to call dashed commands individually, and even required in some
cases (e.g., git-upload-pack for ssh clients).
git-upload-pack is protected pretty well from SIGPIPE shenanigans,
because its stdout all goes through write_or_die, as of cdf4fb8. We
did, long ago, have some EPIPE problems with upload-pack and SSH
clients, but it all predates cdf4fb8.
So I think it's redundant to unblock SIGPIPE in git-upload-pack.
I'll tidy up as Junio recommended, recheck the tests, and submit
an updated patch shortly.
--Patrick