Skip to content

[bug] Avoid memory corruption when the GC frees a cexprtk Symbol_Table - #301

Draft
ptomecek wants to merge 1 commit into
mainfrom
bug/cexprtk-symbol-table-gc
Draft

ptomecek wants to merge 1 commit into
mainfrom
bug/cexprtk-symbol-table-gc

Conversation

@ptomecek

Copy link
Copy Markdown
Collaborator

The test suite segfaults on most CI jobs with pydantic 2.14.4. The cause is in cexprtk: Symbol_Table.__dealloc__ writes through attributes that the type's GC clear function has already set to None, so a Symbol_Table freed by the cyclic garbage collector overwrites memory next to the None object. Whether and where that crashes depends on the interpreter build. Every cexprtk release up to 0.4.2 has this __dealloc__. pydantic 2.14.4 (pydantic/pydantic#14253) lets the collector free reference cycles that pass through a ValidationError, so a Symbol_Table held by a test frame is now freed this way.

When ccflow first imports cexprtk, it removes the GC clear function from Symbol_Table, which is what @cython.no_gc_clear would do upstream. The collector then breaks these cycles through the other objects, and the table is freed by reference counting with its attributes intact. The patch applies only to default CPython builds and only after confirming the slot holds the expected function. It can be removed once cexprtk ships a fix.

Once ccflow has imported cexprtk, the change applies to every Symbol_Table in the process, including tables created by user code.

cexprtk's Symbol_Table.__dealloc__ writes through attributes that its
tp_clear has already reset to None, so freeing a Symbol_Table through the
cyclic garbage collector overwrites memory next to the None object.
pydantic 2.14.4 lets the collector free reference cycles that pass through
a ValidationError, which made this crash the test suite on CI.

Remove Symbol_Table's tp_clear when ccflow first imports cexprtk, so such
cycles are broken through other objects and the table is freed by
reference counting.

Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

    1 files  ±0      1 suites  ±0   2m 28s ⏱️ -9s
1 635 tests +1  1 630 ✅  - 2  5 💤 +3  0 ❌ ±0 
1 722 runs  +1  1 717 ✅  - 2  5 💤 +3  0 ❌ ±0 

Results for commit 90c7091. ± Comparison against base commit 58ae11d.

This pull request skips 4 and un-skips 1 tests.
ccflow.tests.enums.test_pydantic ‑ test_json_schema_csp
ccflow.tests.examples.test_calculator.TestCalculatorCli ‑ test_cli
ccflow.tests.examples.test_etl.TestEtl ‑ test_cli
ccflow.tests.utils.test_tokenize.TestAdversarialFixes ‑ test_function_docstring_still_stripped
ccflow.tests.enums.test_pydantic ‑ test_json_schema_no_csp

♻️ This comment has been updated with latest results.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.63%. Comparing base (58ae11d) to head (90c7091).

Files with missing lines Patch % Lines
ccflow/exttypes/exprtk.py 81.25% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
- Coverage   93.89%   93.63%   -0.27%     
==========================================
  Files         205      205              
  Lines       23126    23158      +32     
  Branches     1517     1519       +2     
==========================================
- Hits        21715    21683      -32     
- Misses       1119     1181      +62     
- Partials      292      294       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant