python / python/cpython

compression.zstd produces a broken archive if nothing is written

Open
#155,286 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.15 3.16 extension-modules type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Creating an empty zstd archive without any .write() calls produces a zero-byte file that fails to open later.

import compression.zstd as zstd
with zstd.open("test.zst", "w") as f:
    pass

with zstd.open("test.zst", "r") as f:
    f.read() # EOFError here. Won't happen if you just pass instead of reading

Traceback (most recent call last):
  File "<python-input-4>", line 2, in <module>
    f.read()
    ~~~~~~^^
  File "compression\zstd\_zstdfile.py", line 173, in read
  File "compression\_common\_streams.py", line 118, in readall
  File "compression\_common\_streams.py", line 99, in read
EOFError: Compressed file ended before the end-of-stream marker was reached

Reference Zstandard v.1.5.7 also won't open the resulting empty file:

zstd.exe -d test.zst -o aa.txt
zstd: test.zst: unexpected end of file

stat test.zst
  File: test.zst
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file

Under same circumstances compression.gzip produces a proper non-empty gzip file that can decompresses into an empty string, which would probably be a desirable behaviour. In the current compression.zstd, you can have that by calling write(b"") instead of pass.

Tested on Windows with recent 3.15rc and 3.14 builds as well as on Linux with 3.10 via backports.zstd.

Python 3.15.0rc1 (tags/v3.15.0rc1:37e98da, Aug  4 2026, 11:37:18) [MSC v.1951 64 bit (AMD64)] on win32
Python 3.14.7 (tags/v3.14.7:823f032, Aug  5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)] on win32
Python 3.10.12 (main, Jun 22 2026, 18:55:27) [GCC 11.4.0] on linux  

Devguide suggested tagging @emmatyping for .zstd bugs.

CPython versions tested on:

3.15, 3.14, 3.10

Operating systems tested on:

Windows, Linux

Linked PRs
  • gh-155342

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with compression/zstd/_zstdfile.py and the read path shown in compression/_common_streams.py. Reproduce the empty context-manager case, then compare it with the empty write case and gzip behavior. Done means an archive created without write() calls can be reopened and read as empty, with regression coverage added; linked PR gh-155342 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.