git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: John Szakmeister <john@szakmeister.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] diff-highlight: exit when a pipe is broken
Date: Sat, 1 Nov 2014 00:04:43 -0400	[thread overview]
Message-ID: <20141101040443.GB8307@peff.net> (raw)
In-Reply-To: <1414753444-68653-1-git-send-email-john@szakmeister.net>

On Fri, Oct 31, 2014 at 07:04:04AM -0400, John Szakmeister wrote:

> While using diff-highlight with other tools, I have discovered that Python
> ignores SIGPIPE by default.  Unfortunately, this also means that tools
> attempting to launch a pager under Python--and don't realize this is
> happening--means that the subprocess inherits this setting.  In this case, it
> means diff-highlight will be launched with SIGPIPE being ignored.  Let's work
> with those broken scripts by explicitly setting up a SIGPIPE handler and exiting
> the process.

My first thought was that this should be handled already by 7559a1b
(unblock and unignore SIGPIPE, 2014-09-18), but after re-reading your
message, it sounds like you are using diff-highlight with non-git
programs?

> +# Some scripts may not realize that SIGPIPE is being ignored when launching the
> +# pager--for instance scripts written in Python.  Setting $SIG{PIPE} = 'DEFAULT'
> +# doesn't work in these instances, so we install our own signal handler instead.

Why doesn't $SIG{PIPE} = 'DEFAULT' work? I did some limited testing and
it seemed to work fine for me. Though I simulated the condition with:

  (
    trap '' PIPE
    perl -e '$|=1; print "foo\n"; print STDERR "bar\n"'
  ) | true

which should not ever print "bar".

Is Python doing something more aggressive, like using sigprocmask to
block the signal? I would think setting $SIG{PIPE} would handle that,
but maybe not in some versions of perl. I dunno. Modern perl
signal-handling is weird, as it catches everything and then defers
propagation until a safe point in the script (if you strace the script
above, you can see that it actually gets EPIPE from the write call!)
I've no clue what implications all that has for the case you're
addressing.

> +sub pipe_handler {
> +    exit(0);
> +}

Can we exit 141 here? If we are part of a pipeline to a pager, it should
not matter either way, but I'd rather not lose the exit code if we can
avoid it (in case of running the script standalone).

> +$SIG{PIPE} = \&pipe_handler;

A minor nit, but would:

  $SIG{PIPE} = sub { ... };

be nicer to avoid polluting the function namespace?

-Peff

  reply	other threads:[~2014-11-01  4:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 11:04 [PATCH] diff-highlight: exit when a pipe is broken John Szakmeister
2014-11-01  4:04 ` Jeff King [this message]
2014-11-04 19:43   ` John Szakmeister

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=20141101040443.GB8307@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=john@szakmeister.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).