Skip to content

Fix possible memory leaks in backend - #373

Merged
ndgrigorian merged 8 commits into
masterfrom
fix-possible-memory-leaks-in-backend
Sep 2, 2026
Merged

Fix possible memory leaks in backend#373
ndgrigorian merged 8 commits into
masterfrom
fix-possible-memory-leaks-in-backend

Conversation

@ndgrigorian

Copy link
Copy Markdown
Collaborator

This PR fixes a few possible memory leaks in the mkl_fft backend caused by possible allocation failures, which could leak structs or the DFTI capsule

These 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_new return an error code and ensure failed iterators are non-iterable and safely destructible.
  • Add allocation checks in mklfft.c.src (mask/shape/stride buffers), propagate DFTI_MEMORY_ERROR, and fix an assert that accidentally used assignment.
  • Make TLS DFTI cache capsule creation exception-safe and centralize MKL status-to-exception mapping (raising MemoryError for DFTI_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.

Comment thread CHANGELOG.md Outdated

@jharlow-intel jharlow-intel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only thing I can really note is the broken markdown github links

Comment thread mkl_fft/_pydfti.pyx Outdated
@antonwolfy antonwolfy added this to the 2.4.0 release milestone Sep 2, 2026
Comment thread mkl_fft/_pydfti.pyx Outdated
Comment thread mkl_fft/_pydfti.pyx Outdated

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ndgrigorian, LGTM

@ndgrigorian
ndgrigorian merged commit d87b5c5 into master Sep 2, 2026
120 of 138 checks passed
@ndgrigorian
ndgrigorian deleted the fix-possible-memory-leaks-in-backend branch September 2, 2026 15:08
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.

4 participants