Repository navigation
Conversation
In the free-threaded build, mutating an fmpz_mat that another thread is reading or writing could free or reallocate the entries while FLINT was using them, which crashed with "double free or corruption" or a segfault (flintlibgh-224). Take a per-object critical section (cython.critical_section) around every fmpz_mat method that reads or writes the entries, and around the places in other modules that read an fmpz_mat (the fmpq_mat, nmod_mat, arb_mat and acb_mat constructors and contains). Binary operations lock both operands. In the default build with the GIL the critical sections compile to nothing. Run test_python_threads in the free-threaded build too and extend it to cover more operations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Refs #224.
In the free-threaded build, mutating a shared
fmpz_matwhile another thread reads or writes it crashes the process. For example,M[i, j] = 10**128andM[i, j] = 0turn an entry into an mpz and back, which allocates and frees it whiledet()or another__setitem__is still using it. The issue suggested per-object locking once Cython supported it. Cython 3.1, the current minimum, hascython.critical_section, so this PR uses it forfmpz_mat:fmpz_matmethods that read or write entries take a critical section onself. Binary operations (==,+,-,*,solve) lock both operands with the two-argument form.fmpq_mat,nmod_mat,arb_matandacb_matconstructors read anfmpz_matargument, so they lock it too, as doarb_mat.containsandacb_mat.contains.nrows/ncolsare not locked because the shape of anfmpz_matnever changes after construction.test_python_threadsused to be skipped on the free-threaded build. It now runs there too, with a fifth thread that runs==,+,-,*, the conversions andrank. Exceptions raised in the threads now fail the test, and it checks the final entries.The other mutable types (the polynomial types, the other matrix types, mpolys, series) are still unprotected. The README and build docs say so. If this approach looks right, I can extend it to them type by type in follow-up PRs. That's why this PR says "Refs" and not "Fixes".
Evidence
Tested on WSL Ubuntu with FLINT 3.6.0, Cython 3.3.2 and CPython 3.14.10, both the free-threaded build and the default build:
test_python_threadscrashed in 5 of 5 runs (double free or corruption (fasttop),malloc(): unaligned fastbin chunk detected, or SIGSEGV). The issue's reproducer crashed in 8 of 10 runs even with only 200 iterations per thread.test_python_threadspassed in 30 of 30 runs, taking about 0.2 s each.python -m flint.test(235 tests, 3186 doctests) andpytest --pyargs flint(1096 tests) pass on both 3.14t and 3.14.-Dpython.allow_limited_api=truewithout warnings.AI use: Claude Code (Claude Opus 5.5) wrote this change and the description, working on behalf of @KainanP. It ran all the tests listed above locally.
🤖 Generated with Claude Code