agronholm / cbor2

Python CBOR (de)serializer with extensive tag support
MIT License
226 stars 59 forks source link

`tests/test_types.py::test_tag_recursive_hash[c]` crashes on Python assertions: `Fatal Python error: _Py_CheckSlotResult: Slot __len__ of type set succeeded with an exception set` #213

Closed mgorny closed 9 months ago

mgorny commented 9 months ago

Things to check first

cbor2 version

5.6.0, 5.6.1, 42bc18174491ccda90d8cfc781585ea13e69cfdb

Python version

3.11.7

What happened?

When CPython is built with assertions enabled, cbor2's C module crashes it while running the test suite:

$ python -m pytest -vs tests/test_types.py::test_tag_recursive_hash
========================================================= test session starts =========================================================
platform linux -- Python 3.11.7, pytest-8.0.0, pluggy-1.4.0 -- /home/mgorny/git/cpython/.venv/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase(PosixPath('/home/mgorny/git/cpython/cbor2/.hypothesis/examples'))
rootdir: /home/mgorny/git/cpython/cbor2
configfile: pyproject.toml
plugins: hypothesis-6.97.4
collected 2 items                                                                                                                     

tests/test_types.py::test_tag_recursive_hash[python] PASSED
tests/test_types.py::test_tag_recursive_hash[c] Fatal Python error: _Py_CheckSlotResult: Slot __len__ of type set succeeded with an exception set
Python runtime state: initialized
RuntimeError: This CBORTag is not hashable because it contains a reference to itself

Current thread 0x00007fec3e332740 (most recent call first):
  File "/home/mgorny/git/cpython/cbor2/tests/test_types.py", line 90 in test_tag_recursive_hash
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/python.py", line 193 in pytest_pyfunc_call
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 102 in _multicall
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 119 in _hookexec
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 501 in __call__
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/python.py", line 1836 in runtest
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 173 in pytest_runtest_call
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 102 in _multicall
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 119 in _hookexec
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 501 in __call__
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 266 in <lambda>
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 345 in from_call
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 265 in call_runtest_hook
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 226 in call_and_report
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 133 in runtestprotocol
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/runner.py", line 114 in pytest_runtest_protocol
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 102 in _multicall
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 119 in _hookexec
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 501 in __call__
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/main.py", line 351 in pytest_runtestloop
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 102 in _multicall
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 119 in _hookexec
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 501 in __call__
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/main.py", line 326 in _main
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/main.py", line 272 in wrap_session
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/main.py", line 319 in pytest_cmdline_main
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 102 in _multicall
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 119 in _hookexec
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 501 in __call__
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 174 in main
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/_pytest/config/__init__.py", line 197 in console_main
  File "/home/mgorny/git/cpython/.venv/lib/python3.11/site-packages/pytest/__main__.py", line 5 in <module>
  File "<frozen runpy>", line 88 in _run_code
  File "<frozen runpy>", line 198 in _run_module_as_main

Extension modules: _cbor2 (total: 1)
Aborted (core dumped)

How can we reproduce the bug?

git clone https://github.com/python/cpython
cd cpython
git checkout v3.11.7
./configure -C --with-assertions
make -j$(nproc)
./python -m venv .venv
. .venv/bin/activate

git clone https://github.com/agronholm/cbor2/
cd cbor2
pip install -e '.[test]'
python -m pytest -vs
agronholm commented 9 months ago

Is there a way to run such a Python build in CI?

mgorny commented 9 months ago

Well, I suppose you could build it as part of the CI process. I'm not aware if anyone is providing prebuilt executables. I think GitHub Actions have some mechanism for caching this stuff but I've never used.

mgorny commented 9 months ago

Thanks! I can confirm that with this change, the module no longer crashes.