git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: Vincent Lefevre <vincent@vinc17.net>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>
Subject: Re: git fails with a broken pipe when one quits the pager
Date: Thu, 04 Feb 2021 01:14:10 +0100	[thread overview]
Message-ID: <87v9b8d6zx.fsf@evledraar.gmail.com> (raw)
In-Reply-To: <20210203152634.GA22673@joooj.vinc17.net>


On Wed, Feb 03 2021, Vincent Lefevre wrote:

> On 2021-02-01 16:44:24 +0100, Ævar Arnfjörð Bjarmason wrote:
>> And then whether it makes sense to ignore SIGPIPE for all users, or
>> e.g. if it's some opt-in setting in some situations that users might
>> want to turn on because they're aware of how their pager behaves and
>> want to work around some zsh mode.
>
> AFAIK, SIGPIPE exists for the following reason. Most programs that
> generate output are not written to specifically handle pipes. So,
> if SIGPIPE did not exist, there would be 2 kinds of behavior:
>   1. The program doesn't check for errors, and still outputs data,
>      wasting time and resources as output will be ignored.
>   2. The program sees that the write() failed and terminates with
>      an error message. However, in most cases, such a failure is
>      not an error: the consumer has terminated either because it
>      no longer needs any input (e.g. with the "head" utility or a
>      pager), or because it has terminated abnormally, in which case
>      the real error is on the side of the consumer. So, the error
>      message from the LHS of the pipe would be annoying.
>
> SIGPIPE solves this issue: the program is simply killed with SIGPIPE.
> In a shell, one gets a non-zero exit code (128 + 13) due to the
> signal, but as being on the left-hand side of the pipe, such a
> non-zero exit code is normally not reported, so that this will not
> annoy the user.
>
> Note 1: Non-zero exit codes from right-hand side are not reported
> either by most shells, but zsh can report them, and this is very
> useful for developers, as programs may fail with a non-zero exit
> code but without an error message. (Reports may also be done by
> looking at the standard $? in some hook.)
>
> Note 2: Failures on the left-hand side are less interesting in practice
> and generally ignored, at least for commands run in interactive shells.
> For scripts, there are various (non-simple) ways to handle them.
>
> Now, I think that in the case (like Git) a program creates a pipe,
> it should use its knowledge to handle SIGPIPE / EPIPE. Either this
> is regarded as an error because the full output is *always expected*
> to be read, in which case there should be an error message in addition
> to the usual non-zero exist status (not necessarily 141), or this is
> regarded as OK (if there is a real failure, this is on the side of
> the consumer). In the case of Git, the consumer is documented to be
> a pager, which obviously may not read the full output (e.g. for the
> GCC repository, "git log" returns more than 3 million lines, back to
> the year 1988, while one is generally interested in the latest changes
> only). If the user wants to pipe to something else, he can always use
> an explicit pipe.

SIGPIPE exists because *nix systems are composable, so you can make
something useful by stringing together unrelated programs via files and
pipes, and with exit codes and signal mostly everyone's happy.

I follow what you're saying right until the point of arguing that
because either your shell or POSIX shells in general have decided to
either be sloppy or overzelous in how they show you some
information. That we should use their behavior as a guide in
pro-actively suppressing our own exit code.

And that's not because I think (to the tune of Monty Python...) that
every exit code is sacret. It's because when we invoke a pager handing
it data is *the* thing we're doing. If we can fully hand it over, great,
if not, let's tell the user with the appropriate exit code.

Yeah it's annoying with zsh's PRINT_EXIT_VALUE, but the same is true of
POSIX "set -e". Not every shell option is meant for general use. The
shell is very forgiving of things like pipe failures by default for a
reason.

But "connected to a terminal" (isatty(1)) and "invoked by Vincent's zsh
instance" aren't the same thing. And I think it makes sense to be
conservative in preserving exit codes.

In the early days of git complaining about "Broken pipe" in the exact
same scenario was the default behavior of bash's overzelous reporting,
as you can read about starting here:
https://lore.kernel.org/git/?q=%22Broken+pipe%22+bash&o=-1

AFAICT that changed by default in bash 3.1, released in 2005-12-08. It
was a FAQ in the early days, now nobody cares.

Have you reported this as a bug to zsh? I think it's likely that the
motivation for wanting this squashed in git is going to be as transitory
as bash's once-default verbosity was.

I also tested "hg log", it behaves the same way, although interestingly
they cast SIGPIPE to 255 in their exit code.


  reply	other threads:[~2021-02-04  0:15 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15 16:15 git fails with a broken pipe when one quits the pager Vincent Lefevre
2021-01-29 23:48 ` [PATCH] pager: exit without error on SIGPIPE Denton Liu
2021-01-30  8:29   ` Johannes Sixt
2021-01-30 12:52     ` Johannes Sixt
2021-02-01 15:03   ` Ævar Arnfjörð Bjarmason
2021-02-01 17:47     ` Junio C Hamano
2021-02-01 19:52       ` Ævar Arnfjörð Bjarmason
2021-02-01 20:55         ` Junio C Hamano
2021-02-02  2:05           ` Ævar Arnfjörð Bjarmason
2021-02-02  4:45             ` Junio C Hamano
2021-02-02  5:25               ` Junio C Hamano
2021-02-02  7:45                 ` Johannes Sixt
2021-02-02 20:13                   ` Junio C Hamano
2021-02-02 22:15                     ` Johannes Sixt
2021-02-02 22:21                       ` Junio C Hamano
2021-02-03 17:07                         ` Johannes Sixt
2021-02-03 18:12                           ` Junio C Hamano
2021-02-04 15:10                           ` Vincent Lefevre
2021-02-03  2:45                 ` Ævar Arnfjörð Bjarmason
2021-02-03  2:54                   ` Junio C Hamano
2021-02-03  3:36                     ` Ævar Arnfjörð Bjarmason
2021-02-03 17:19                     ` Johannes Sixt
2021-01-31  1:47 ` git fails with a broken pipe when one quits the pager Ævar Arnfjörð Bjarmason
2021-01-31  3:36   ` Vincent Lefevre
2021-01-31  3:47     ` Vincent Lefevre
2021-01-31 20:49     ` Ævar Arnfjörð Bjarmason
2021-02-01 10:34       ` Vincent Lefevre
2021-02-01 11:33         ` Chris Torek
2021-02-01 12:36           ` Vincent Lefevre
2021-02-01 12:53             ` Chris Torek
2021-02-01 15:17               ` Vincent Lefevre
2021-02-01 15:00           ` Ævar Arnfjörð Bjarmason
2021-02-01 12:10         ` Ævar Arnfjörð Bjarmason
2021-02-01 14:48           ` Vincent Lefevre
2021-02-01 15:44             ` Ævar Arnfjörð Bjarmason
2021-02-01 22:16               ` Johannes Sixt
2021-02-03  2:48                 ` Ævar Arnfjörð Bjarmason
2021-02-03 17:11                   ` Johannes Sixt
2021-02-03 15:26               ` Vincent Lefevre
2021-02-04  0:14                 ` Ævar Arnfjörð Bjarmason [this message]
2021-02-04 15:38                   ` Vincent Lefevre
2021-02-01 14:49           ` [PATCH 0/3] pager: test for exit behavior & trace2 bug fix Ævar Arnfjörð Bjarmason
2021-02-02  1:59             ` [PATCH v2 0/5] " Ævar Arnfjörð Bjarmason
2021-02-02  1:59             ` [PATCH v2 1/5] pager: refactor wait_for_pager() function Ævar Arnfjörð Bjarmason
2021-02-02  1:59             ` [PATCH v2 2/5] pager: test for exit code with and without SIGPIPE Ævar Arnfjörð Bjarmason
2021-02-02  8:50               ` Denton Liu
2021-02-05  7:47               ` Johannes Sixt
2021-02-02  1:59             ` [PATCH v2 3/5] run-command: add braces for "if" block in wait_or_whine() Ævar Arnfjörð Bjarmason
2021-02-02  2:00             ` [PATCH v2 4/5] pager: properly log pager exit code when signalled Ævar Arnfjörð Bjarmason
2021-02-05  7:58               ` Johannes Sixt
2021-02-05 11:37                 ` Junio C Hamano
2021-02-02  2:00             ` [WIP/PATCH v2 5/5] WIP pager: respect exit code of pager over SIGPIPE Ævar Arnfjörð Bjarmason
2021-02-01 14:49           ` [PATCH 1/3] pager: test for exit code Ævar Arnfjörð Bjarmason
2021-02-01 14:49           ` [PATCH 2/3] pager: refactor wait_for_pager() function Ævar Arnfjörð Bjarmason
2021-02-01 14:49           ` [PATCH 3/3] pager: properly log pager exit code when signalled Ævar Arnfjörð Bjarmason
2021-02-01 18:07             ` Junio C Hamano
2021-02-01 19:21               ` Ævar Arnfjörð Bjarmason
2021-02-01 18:15             ` Junio C Hamano
2021-02-01 19:23               ` Ævar Arnfjörð Bjarmason
2021-02-01 22:04       ` git fails with a broken pipe when one quits the pager Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v9b8d6zx.fsf@evledraar.gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=vincent@vinc17.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).