Fix possible memory leaks in backend - #373
Conversation
… failure Also fixes a typo and makes _direct_fftnd check status
There was a problem hiding this comment.
🟢 Approval recommended
The changes are tightly scoped, consistently propagate allocation failures, and I did not find any remaining unchecked call sites or new leak paths in the modified regions.
Pull request overview
This PR hardens mkl_fft’s C/Cython backend against allocation-failure paths to avoid leaking iterator/capsule state and to surface memory exhaustion as a proper Python MemoryError, aligning with the PR’s goal of preventing “poisoned” thread-local cache state under free-threaded stress.
Changes:
- Make
multi_iter_new/multi_iter_masked_newreturn an error code and ensure failed iterators are non-iterable and safely destructible. - Add allocation checks in
mklfft.c.src(mask/shape/stride buffers), propagateDFTI_MEMORY_ERROR, and fix anassertthat accidentally used assignment. - Make TLS DFTI cache capsule creation exception-safe and centralize MKL status-to-exception mapping (raising
MemoryErrorforDFTI_MEMORY_ERROR).
File summaries
| File | Description |
|---|---|
| mkl_fft/tests/test_fftnd.py | Switches a few assertion messages to f-strings (no behavior change). |
| mkl_fft/src/multi_iter.h | Iterator constructors now fail fast on allocation errors and are safe to free after partial initialization. |
| mkl_fft/src/mklfft.c.src | Adds allocation-failure handling to prevent leaks and correctly propagates memory errors; fixes an incorrect assert. |
| mkl_fft/_pydfti.pyx | Prevents TLS capsule caching of failed allocations and maps MKL memory errors to Python MemoryError. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
jharlow-intel
left a comment
There was a problem hiding this comment.
only thing I can really note is the broken markdown github links
antonwolfy
left a comment
There was a problem hiding this comment.
Thank you @ndgrigorian, LGTM
This PR fixes a few possible memory leaks in the
mkl_fftbackend caused by possible allocation failures, which could leak structs or the DFTI capsuleThese were caught during free-threaded Python review, with some surfacing in free-threaded test runs, where a thread could fail to allocate the capsule and then cache the failed allocation, leading to the entire thread becoming poisoned