From 674f35e3d236b352ebb45c8b8446474240e033dc Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 3 Nov 2026 13:08:03 +0100 Subject: [PATCH] Fix broken pipe error when piping into programs using the CLI. --- CHANGELOG.rst | 2 ++ src/zlib_ng/gzip_ng.py | 3 +++ tests/test_gzip_ng.py | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e30b5ea..0bf5722 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,8 @@ Changelog version 1.0.1-dev ----------------- + Update included zlib-ng distribution to 2.3.3. ++ Fix BrokenPipeError when piping ``python -m isal.igzip`` into ``head``, + ``less`` or similar programs. + Fix ``gzip_ng_threaded`` writers that waited forever when writing to the underlying file failed, for example, because the disk was full. A later call to ``write()``, ``flush()``, or ``close()`` raises the error. After an error, diff --git a/src/zlib_ng/gzip_ng.py b/src/zlib_ng/gzip_ng.py index f24bdbe..c2afec2 100644 --- a/src/zlib_ng/gzip_ng.py +++ b/src/zlib_ng/gzip_ng.py @@ -304,6 +304,9 @@ def main(): READ_BUFFER_SIZE = args.buffer_size try: shutil.copyfileobj(in_file, out_file, args.buffer_size) + except BrokenPipeError: + # Do not throw errors when a pipe to head or less closes. + pass finally: if in_file is not sys.stdin.buffer: in_file.close() diff --git a/tests/test_gzip_ng.py b/tests/test_gzip_ng.py index 171bd84..41eb954 100644 --- a/tests/test_gzip_ng.py +++ b/tests/test_gzip_ng.py @@ -15,6 +15,7 @@ import re import shutil import struct +import subprocess import sys import tempfile import zlib @@ -496,3 +497,14 @@ def test_adverserial_header(): gzip_file.read() finally: os.remove(tmpfile.name) + + +@pytest.mark.skipif(sys.platform != "linux", reason="bash required") +def test_no_broken_pipe(): + subprocess.run( + [ + "bash", "-c", "-e", "-o", "pipefail", + f"{sys.executable} -m zlib_ng.gzip_ng -cd {TEST_FILE} | head -n 10" + ], + check=True + )