Skip to content

Use critical sections to make fmpz_mat thread-safe in the free-threaded build - #456

Open
KainanP wants to merge 2 commits into
flintlib:mainfrom
KainanP:fmpz-mat-critical-section
Open

KainanP wants to merge 2 commits into
flintlib:mainfrom
KainanP:fmpz-mat-critical-section

Conversation

@KainanP

@KainanP KainanP commented Nov 28, 2026 •

Copy link
Copy Markdown

Refs #224.

In the free-threaded build, mutating a shared fmpz_mat while another thread reads or writes it crashes the process. For example, M[i, j] = 10**128 and M[i, j] = 0 turn an entry into an mpz and back, which allocates and frees it while det() or another __setitem__ is still using it. The issue suggested per-object locking once Cython supported it. Cython 3.1, the current minimum, has cython.critical_section, so this PR uses it for fmpz_mat:

  • The fmpz_mat methods that read or write entries take a critical section on self. Binary operations (==, +, -, *, solve) lock both operands with the two-argument form.
  • The fmpq_mat, nmod_mat, arb_mat and acb_mat constructors read an fmpz_mat argument, so they lock it too, as do arb_mat.contains and acb_mat.contains.
  • Python-level work (argument conversion, allocating results) stays outside the sections where possible, so a section isn't suspended halfway through a FLINT call.
  • nrows/ncols are not locked because the shape of an fmpz_mat never changes after construction.
  • In the default (GIL) build, Cython compiles the critical sections to nothing.

test_python_threads used to be skipped on the free-threaded build. It now runs there too, with a fifth thread that runs ==, +, -, *, the conversions and rank. 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:

  • Before the change (3.14t): the new test_python_threads crashed 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.
  • After the change (3.14t):
    • test_python_threads passed in 30 of 30 runs, taking about 0.2 s each.
    • The issue's reproducer with 100 000 iterations per thread passed in 50 of 50 runs.
  • Full suite: python -m flint.test (235 tests, 3186 doctests) and pytest --pyargs flint (1096 tests) pass on both 3.14t and 3.14.
  • Limited API: the changed modules also build with -Dpython.allow_limited_api=true without 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

KainanP and others added 2 commits November 28, 2026 03:40
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

No deployments
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