Skip to content

fix(python): stop pickle protocols 0 and 1 from aborting the interpreter - #242

Merged
gouyt13 merged 1 commit into
dev/0.9from
fix/pickle-protocol
Nov 15, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
fix/pickle-protocol

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 15, 2026

Copy link
Copy Markdown
Member

Problem

pickle.dumps(obj, protocol=0) or protocol=1 on any rabitqlib object aborts the Python process:

terminate called after throwing an instance of 'std::runtime_error'
  what():  instance allocation failed: new instance has no pybind11-registered base types

The cause is in pybind11:

  1. For protocols 0 and 1, object.__reduce_ex__ falls back to copyreg._reduce_ex.
  2. That function calls pybind11_object(obj), the common pybind11 base class.
  3. pybind11_object_new reaches instance::allocate_layout(), which throws through pybind11_fail.
  4. Nothing in the C call path catches the exception, so the process terminates.

It reproduces with a standalone two-class pybind11 module on pybind11 3.1.0 and master (c68d56a), with CPython 3.11, 3.12 and 3.13. pybind11 documents protocols 0 and 1 as unsupported, and the upstream report, pybind11#3745, is labelled wontfix. The default protocol is 5, so only code that asks for protocol 0 or 1 is affected.

Fix

Every extension class and enum gets a __reduce_ex__ (reduce_ex_with_protocol_2 in python_bindings/bindings_common.hpp) that calls object.__reduce_ex__(self, max(protocol, 2)):

  • quant.Rotator, quant.Query and the two enums now pickle with every protocol.
  • The index, k-means, parameter and iteration-statistics classes raise TypeError: cannot pickle ... with protocols 0 and 1, as they already did with protocol 2 and higher.
  • Protocols 2 to 5 produce the same bytes as before, and pickles load in both directions between this build and the previous one. That includes protocol-0 pickles written by this build.
  • copy.copy and copy.deepcopy are unchanged.

The 12 types are the three index classes, both parameter classes, KMeansIterationStats, the two native k-means classes, quant.Rotator, quant.Query, MetricType and RotatorType. Release note added.

Tests

New tests/python/test_pickle_protocols.py:

  • Child-process test: a crash would otherwise take down pytest, so this test runs in a child process. It pickles every class with protocols 0 and 1:
    • Rotator, Query (with and without a centroid) and the enums must round-trip with identical state;
    • the other classes must raise TypeError.
  • Static test: every pybind11 class registered by rabitqlib, found through pybind11_object.__subclasses__(), must define __reduce_ex__, so a new class cannot miss it.
  • Mutation checks:
    • Both tests fail on the previous build.
    • Removing __reduce_ex__ from any single class makes the test fail.

Verification

Local, Linux x86-64, Python 3.13, scratch wheel of this branch:

  • pytest: 1180 passed, 18 skipped. 16 of the skips are test_cpp_clustering_examples.py, which needs the C++ example binaries in bin/; the other 2 are the macOS and Linux ARM64 wheel tests.
  • test_editor_support.py, test_mypy.py, test_pyright.py: 18 passed.
  • check-python.sh, check-format.sh, full check-tidy.sh (clang-tidy 23), mkdocs build --strict, git diff --check.
  • The workaround was also checked on Python 3.11 and 3.12 with the standalone module.

pickle.dumps(obj, protocol=0) or protocol=1 on any rabitqlib object
aborted the process. Those protocols reduce through copyreg._reduce_ex,
which calls pybind11_object(obj); its constructor throws a C++ exception
through the C API, so the interpreter terminates (pybind11 issue 3745,
reproduced with pybind11 3.1.0 and master).

Every extension class and enum now defines __reduce_ex__, which uses the
protocol-2 reduction for protocols 0 and 1. quant.Rotator, quant.Query and
the enums pickle with every protocol; the other classes raise TypeError,
as they already did with protocol 2 and higher. Protocols 2 to 5 produce
the same bytes as before.

A child-process test covers protocols 0 and 1 on every class, and a static
test checks that each extension class defines __reduce_ex__.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 15, 2026
@gouyt13
gouyt13 merged commit afc0d0e into dev/0.9 Nov 15, 2026
25 checks passed
@gouyt13
gouyt13 deleted the fix/pickle-protocol branch November 15, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fixed Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant