Repository navigation
fix(python): stop pickle protocols 0 and 1 from aborting the interpreter - #242
Merged
Merged
Conversation
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__.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
pickle.dumps(obj, protocol=0)orprotocol=1on anyrabitqlibobject aborts the Python process:The cause is in pybind11:
object.__reduce_ex__falls back tocopyreg._reduce_ex.pybind11_object(obj), the common pybind11 base class.pybind11_object_newreachesinstance::allocate_layout(), which throws throughpybind11_fail.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_2inpython_bindings/bindings_common.hpp) that callsobject.__reduce_ex__(self, max(protocol, 2)):quant.Rotator,quant.Queryand the two enums now pickle with every protocol.TypeError: cannot pickle ...with protocols 0 and 1, as they already did with protocol 2 and higher.copy.copyandcopy.deepcopyare unchanged.The 12 types are the three index classes, both parameter classes,
KMeansIterationStats, the two native k-means classes,quant.Rotator,quant.Query,MetricTypeandRotatorType. Release note added.Tests
New
tests/python/test_pickle_protocols.py:Rotator,Query(with and without a centroid) and the enums must round-trip with identical state;TypeError.rabitqlib, found throughpybind11_object.__subclasses__(), must define__reduce_ex__, so a new class cannot miss it.__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 aretest_cpp_clustering_examples.py, which needs the C++ example binaries inbin/; 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, fullcheck-tidy.sh(clang-tidy 23),mkdocs build --strict,git diff --check.