Not unbundling Zstandard from py-zstandard

Written early in the morning in English • Tags: , , ,

After archivers/py-brotli turned out to need just one environment variable to stop compiling its own copy of Brotli, I tried the same with archivers/py-zstandard. That took some effort and ended somewhere else.

py-zstandard builds two extension modules, a C backend and a CFFI backend, and both embed the bundled zstd.c. This matters more than it used to: devel/py-mercurial recently stopped building its own zstd module and depends on py-zstandard instead, so this package is now where Mercurial’s zstd comes from.

Following Debian

The C backend has a --system-zstd switch. The CFFI backend does not. Debian solves that with patches from Gentoo, submitted upstream as pull request 249, plus one of their own to make the header parsing order deterministic. Debian’s changelog gives a reason for the effort: the bundled zstd 1.5.7 is affected by CVE-2026-124851, an information leak in the streaming decoder that I have been tracking.

I adapted those patches for pkgsrc, with an option to keep the bundled copy. Both backends built against archivers/zstd 1.6.0 and passed their tests, and Mercurial was happy with the result.

There is a cost. multi_compress_to_buffer() and multi_decompress_to_buffer() use zstd’s private thread pool, which the shared library does not export, so they are compiled out of any system-zstd build. Compression with threads=N is not affected. None of the packages depending on py-zstandard use the two methods (I checked Mercurial, Buildbot, urllib3 and Taisei), but it is still a different module from the one upstream ships.

Not unbundling it

Before committing I asked Thomas Klausner, who had made the recent Mercurial change and knows the Python packages well. Upstream only supports the bundled copy and is not going to change that, so we are not carrying the patches.

Conveniently, version 0.26.0 came out the same evening with the bundled zstd upgraded to 1.6.0. I updated the package to that instead.

A test target that tests something

One part of the detour was worth keeping. The package Makefile had a note saying the tests produce 28 errors. They did, because pytest ran in the source directory and imported the unbuilt zstandard package from there, so nearly every test module failed at import.

The package now copies the tests out of the source tree and runs them against the built module, once per backend:

do-test:
        ${RUN} ${MKDIR} ${WRKDIR}/t && \
        ${CP} -R ${WRKSRC}/tests ${WRKDIR}/t/
.for policy in cext cffi
        ${RUN} cd ${WRKDIR}/t && ${SETENV} ${TEST_ENV} \
        PYTHONPATH=$$(echo ${WRKSRC}/build/lib.*) \
        PYTHON_ZSTANDARD_IMPORT_POLICY=${policy} \
        pytest-${PYVERSSUFFIX}
.endfor

With 0.26.0 that is 251 tests passing on the C backend and 226 on CFFI. The fuzzing tests are skipped unless ZSTD_SLOW_TESTS is set, which I have not tried.

The system-zstd change is available as a diff against 0.25.0, should upstream ever reconsider.

Many thanks to Thomas, who has always been kind and generous with his help whenever I have had questions, and who puts a great deal of effort into keeping all of pkgsrc in shape.

Add a comment