BUG: Fix reference leaks in ArrayMethod resolvers and scalar paths - #119
Open
SwayamInSync wants to merge 2 commits into
Open
BUG: Fix reference leaks in ArrayMethod resolvers and scalar paths#119SwayamInSync wants to merge 2 commits into
SwayamInSync wants to merge 2 commits into
Conversation
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.
Closes #115
The ArrayMethod resolvers acquired descriptors slot by slot and returned on the first failure, leaving the earlier slots holding references. They now NULL-initialize
loop_descrsand fail through a sharedquad_resolve_descrs_fail()helper that clears every acquired slot.This covers the 17 resolvers with a failure path:
ldexpNotImplementedErrorbranchesfrexp, and the unary property resolverFive descriptor lookups were also unchecked and reported success with a NULL entry: the
ldexpNPY_INTP,frexpNPY_INT32and unary-propertyNPY_BOOLlookups, andPyArray_GetDefaultDescr()in both NumPy cast resolvers. These now fail through the same path.The cast call sites are where this leaks today.
convert_datatype.canddtype_transfer.cneither zero-initializeloop_descrsnor release it on error, while the ufunc path inufunc_object.chappens to clean up on our behalf. The contract puts it on the resolver either way.Reference leaks elsewhere
Auditing the rest of the extension for the same class of bug turned up four
leaks:
as_integer_ratio()leaked the numerator and denominator on every call, sincePyTuple_Pack()increfs its items. 20,000 calls leaked 40,002 blocks.QuadPrecDType()built aQuadPrecisionscalar only to read its backend off it and never released it: one object per dtype construction.TypeErrorpath.init_quadprec_dtype()leaked the cast specs when registration failed.And five failure paths that crash rather than raise:
QuadPrecision("\ud800")segfaulted:PyUnicode_AsUTF8()returns NULL for lone surrogates and the NULL reached the parser. It now raisesUnicodeEncodeError, matchingnp.longdouble.QuadPrecision_raw_new()without a NULL check.init_casts()caughtint, sostd::bad_allocfromnewreachedstd::terminateinstead of raisingMemoryError.MemoryErrorand carried on with a NULL lock. Module init now fails.PyModule_AddObject()steals a reference that was never owned; replaced withPyModule_AddObjectRef().The matmul scratch buffers, the StringDType allocator pairing, Dragon4's thread-local scratch and the ufunc registration paths were checked and are already correct.
Testing
No new tests. Every remaining resolver error path needs an allocation failure to reach, and the one branch reachable without OOM (matmul with a mismatched
out=backend) is cleaned up by NumPy itself, so a refcount test would pass without the fix as well.The leaks above were measured with
sys.getallocatedblocks(): 200,000 iterations ofas_integer_ratio()plusQuadPrecDType()went from 400,000 leaked blocks to 1. Existing suite passes (7819 passed, 4 skipped, 18 xfailed).