git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] diff-highlight: exit when a pipe is broken

From
John Szakmeister <john@szakmeister.net>
Date
Oct 31, 2014, 11:04 UTC
Message-ID
<1414753444-68653-1-git-send-email-john@szakmeister.net>

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.

Signed-off-by: John Szakmeister <john@szakmeister.net>
---
 contrib/diff-highlight/diff-highlight | 9 +++++++++
 1 file changed, 9 insertions(+)
diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight
index c4404d4..dfcc35a 100755
--- a/contrib/diff-highlight/diff-highlight
+++ b/contrib/diff-highlight/diff-highlight
@@ -14,6 +14,15 @@ my @removed;
 my @added;
 my $in_hunk;
 
+# 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.
+sub pipe_handler {
+    exit(0);
+}
+
+$SIG{PIPE} = \&pipe_handler;
+
 while (<>) {
 	if (!$in_hunk) {
 		print;
-- 
2.0.1
Next: Jeff King
Message 1 of 3 in “diff-highlight: exit when a pipe is broken”
  1. diff-highlight: exit when a pipe is brokenJohn Szakmeister, Oct 31, 2014
  2. Jeff KingNov 1, 2014
  3. John SzakmeisterNov 4, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.