Fix Metal buffer leak (PyObjC ownership of MTLDevice new* methods) - #45
Open
neurolabusc wants to merge 2 commits into
Open
neurolabusc wants to merge 2 commits into
neurolabusc wants to merge 2 commits into
Conversation
…ethods PyObjC's Metal bindings have no ownership metadata for the MTLDevice protocol's new* selectors, so the +1 return was treated as autoreleased and never released. Every per-batch MTLBuffer leaked for the life of the process (300k seeds: 16.5 GB peak footprint -> 6.1 GB, 9.8 GB max RSS -> 5.3 GB). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Fork PRs have no write token for the org GHCR package, so the pull_request build always failed at the push step (see dipy#43 as well). Co-Authored-By: Claude Fable 5.1 <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.
Problem
Every per-batch
MTLBufferon the Metal backend leaked for the life of the process. Tracking 300k HARDI seeds reached 16.5 GB peak memory footprint (9.8 GB max RSS); the same leak was reported against the dipy port in dipy/dipy#4033 (19.4 GB peak, 16.2 GB retained for 588k seeds).Cause
PyObjC's Metal bindings carry no ownership metadata for the
MTLDeviceprotocol'snew*selectors (newBufferWithLength:options:,newBufferWithBytes:length:options:, etc.). PyObjC therefore treats the +1 return as autoreleased, retains it once more, and never releases it.device.currentAllocatedSize()never drops afterdel buf+gc.collect(); autorelease pools make no difference.Fix
Register
already_retainedmetadata for the sixnew*selectors at import ofcuslines/metal/mutils.py.registerMetaDataForSelectorreplaces the inferred metadata, so the pointer and error out-arguments are restated.Results (M-series, 300k seeds, HARDI)
Fixed-seed runs produce identical streamline counts before and after.
🤖 Generated with Claude Code