Repository navigation
Conversation
|
I confirm this PR addresses the bug. |
|
It looks like some test is stalled with this PR. Any hint? |
|
All stall processes are when testing 3.14t, i.e. the free-threaded version. |
|
I reproduced the stall locally on Python 3.14t with the CI test command. The new regression test passed on its own, but under |
|
Apparently the fact the tests dead-lock in thread-free version of python is a different issue related to the I can propose some locking to protect this function if needed, but maybe @xuu33030 prefers doing it on his own. |
|
Thanks for offering, @kif. Would you like to take the |
|
I think I will submit a separated PR ... maybe even open another issue since what we see here is just highlighting an issue already pre-existing. |
|
I did that on #572 I reviewed this diff against the interpreter code, and the approach looks correct to me. In the serial path, get_temps_space() writes the temporaries' pointers into self->mem, and free_temps_space() frees them later. When two threads share one compiled object, each overwrites the other's pointers, so the same blocks get freed twice. That matches the double free / corrupted size aborts reported in #569. A per-object lock is the simplest way to fix it. Some remarks, none of them blocking:
So a shared compiled expression running a sum()/prod(), or working on small arrays, can corrupt the heap even with several numexpr threads. Setting NUMEXPR_NUM_THREADS>=2, the workaround suggested in #569, is therefore not a complete fix. Since this PR takes the lock at the top of NumExpr_run(), it covers all of these cases. It may be worth saying so in the PR description, and possibly adding a reduction variant to the regression test (e.g. NumExpr("sum(a*b)")).
Apart from point 2, which I'd suggest addressing here, I think this is ready to merge once the CI on the fork has been approved and runs green, including 3.14t. |
Summary
NumExprobject with a per-object lockProblem
A compiled
NumExprobject stores mutable execution buffers on the object. When the same object is called concurrently whileNUMEXPR_NUM_THREADS=1, multiple Python threads can enter the serial interpreter path at once and overwrite that shared state, causing incorrect results or heap corruption.The existing global
parallel_mutexonly protects the multi-threaded interpreter path and therefore does not cover this case.Verification
SIGABRTon macOS arm64 / Python 3.13 / NumPy 2.5.3.python -m pytest numexpr/tests/test_numexpr.py::test_threading::test_shared_compiled_expression -q— 1 passedpython -m pytest -q— 6043 passedNUMEXPR_NUM_THREADS=1— printsOKgit diff --check— cleanCloses #569