I've run into a similar issue in Rust with SIGPIPE. The compiler adds the call to signal() to ignore SIGPIPEs when a Rust program starts. Apparently this was to stop a listening server from exiting when a client closed their connection. But in my book I'm teaching Rust by rewriting a classic BSD utility (cat, head, wc, ...) in Rust and none of those utilities have to care about broken pipes because they don't ignore the signal and the OS silently kills the process. Instead, if a Rust program doesn't handle a write failing with a EPIPE error then the process panics and exits with a backtrace. The worst part is that Rust standard library doesn't include a module for handling signals so it's not possible to undo the signal() call ignoring SIGPIPEs without using another crate.<p>In the end it's fine and you just have to handle broken pipes but it adds a lot of boilerplate to small CLI programs that in C just work as expected. Another twist (and possible further subtle bug) is that most shells set the exit status of a program that exits from a broken pipe to 141 (128 + the signal number, in this case 13). So when you catch the pipe you can exit the process with a status of 141, but that's the shell's behaviour and there's no safe way to fake an exit status.
The "fix" also has a bug - `preexec_fn` causes nasal demons if the current process has threads.<p>Fortunately, Python 3.2 added the `restore_signals` argument for exactly this purpose, and it is enabled by default. The (2010) in the title is quite relevant.
> This code has a bug<p>From what I see, the code should be expected to work without requiring workarounds; instead, it is one of following:<p>- a python design problem<p>- gzip signal handling bug<p>- a flaw in linux pipe spec
Maybe better to use something like <a href="https://docs.python.org/3/library/tarfile.html" rel="nofollow noreferrer">https://docs.python.org/3/library/tarfile.html</a> instead of calling some "outside" utilities?<p>This might be safer (command injection) and maybe more reliable.<p>However I don't know what I would have chosen and hindsight is always 20/20 and there might be other external commands and requirements...