Skip to content

Commit 10cc987

Browse files
committed
Fix: leaking handle in getbuffer wrapper
1 parent 92ba953 commit 10cc987

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/HPyExternalFunctionNodes.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodes.HPyGetNativeSpacePointerNode;
7171
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodes.PCallHPyFunction;
7272
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyAllAsHandleNodeGen;
73-
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyAsPythonObjectNodeGen;
73+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyEnsureHandleNodeGen;
7474
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyGetBufferProcToSulongNodeGen;
7575
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyGetNativeSpacePointerNodeGen;
7676
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNodesFactory.HPyGetSetGetterToSulongNodeGen;
@@ -1588,12 +1588,14 @@ static final class HPyGetBufferRootNode extends HPyMethodDescriptorRootNode {
15881588
@Child private InteropLibrary valueLib;
15891589
@Child private PCallCapiFunction callGetByteArrayTypeId;
15901590
@Child private PCallCapiFunction callFromTyped;
1591-
@Child private HPyAsPythonObjectNode asPythonObjectNode;
1591+
@Child private HPyEnsureHandleNode asPythonObjectNode;
15921592
@Child private FromCharPointerNode fromCharPointerNode;
15931593
@Child private CastToJavaStringNode castToJavaStringNode;
15941594
@Child private GetIntArrayNode getIntArrayNode;
15951595
@Child private PRaiseNode raiseNode;
15961596

1597+
@CompilationFinal private ConditionProfile isAllocatedProfile;
1598+
15971599
@TruffleBoundary
15981600
public HPyGetBufferRootNode(PythonLanguage language, String name) {
15991601
super(language, name, HPyCheckPrimitiveResultNodeGen.create(), HPyGetBufferProcToSulongNodeGen.create());
@@ -1655,7 +1657,7 @@ private CExtPyBuffer createPyBuffer(GraalHPyContext hpyContext, Object bufferPtr
16551657
}
16561658
if (asPythonObjectNode == null) {
16571659
CompilerDirectives.transferToInterpreterAndInvalidate();
1658-
asPythonObjectNode = insert(HPyAsPythonObjectNodeGen.create());
1660+
asPythonObjectNode = insert(HPyEnsureHandleNodeGen.create());
16591661
}
16601662
if (fromCharPointerNode == null) {
16611663
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -1687,7 +1689,11 @@ private CExtPyBuffer createPyBuffer(GraalHPyContext hpyContext, Object bufferPtr
16871689
ownerObj = ptrLib.readMember(ownerObj, GraalHPyHandle.I);
16881690
Object owner = null;
16891691
if (!valueLib.isNull(ownerObj)) {
1690-
owner = asPythonObjectNode.execute(hpyContext, ownerObj);
1692+
GraalHPyHandle ownerHandle = asPythonObjectNode.execute(hpyContext, ownerObj);
1693+
// Since we are now the owner of the handle and no one else will ever use it, we
1694+
// need to close it.
1695+
ownerHandle.close(hpyContext, ensureIsAllocatedProfile());
1696+
owner = ownerHandle.getDelegate();
16911697
}
16921698

16931699
int ndim = castToInt(ptrLib.readMember(bufferPtr, "ndim"));
@@ -1727,6 +1733,14 @@ private CExtPyBuffer createPyBuffer(GraalHPyContext hpyContext, Object bufferPtr
17271733
}
17281734
}
17291735

1736+
private ConditionProfile ensureIsAllocatedProfile() {
1737+
if (isAllocatedProfile == null) {
1738+
CompilerDirectives.transferToInterpreterAndInvalidate();
1739+
isAllocatedProfile = ConditionProfile.create();
1740+
}
1741+
return isAllocatedProfile;
1742+
}
1743+
17301744
private int castToInt(Object value) {
17311745
if (valueLib.fitsInInt(value)) {
17321746
try {

0 commit comments

Comments
 (0)