Skip to content

Commit 2796f64

Browse files
fangererqunaibit
authored andcommitted
Don't fail in GcNativePtrToPythonNode if object was collected
1 parent d7c2cc8 commit 2796f64

File tree

1 file changed

+10
-6
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions

1 file changed

+10
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,11 @@ Object doNonWrapper(long pointer, boolean stealing,
16841684
* Very similar to {@link NativePtrToPythonNode}, this node resolves a native pointer (given as
16851685
* Java {@code long}) to a Python object. However, it will never create a fresh
16861686
* {@link PythonAbstractNativeObject} for a native object (it will only return one if it already
1687-
* exists).
1687+
* exists). Also, this node won't fail if a tagged pointer is given and the underlying managed
1688+
* object was collected in the meantime. This is because it may happen that the native object
1689+
* stub of a managed object is in the GC list and while processing it (e.g. replicating the
1690+
* native references), the Java GC may collect it. In such cases, we don't want to fail but
1691+
* return {@code null}.
16881692
*/
16891693
@GenerateUncached
16901694
@GenerateInline
@@ -1709,15 +1713,15 @@ static Object doLong(Node inliningTarget, long pointer,
17091713
int idx = readI32Node.read(HandlePointerConverter.pointerToStub(pointer), CFields.GraalPyObject__handle_table_index);
17101714
PythonObjectReference reference = nativeStubLookupGet(nativeContext, pointer, idx);
17111715
if (reference == null) {
1716+
/*
1717+
* This should really not happen since it most likely means that we accessed
1718+
* free'd memory to read the handle table index.
1719+
*/
17121720
CompilerDirectives.transferToInterpreterAndInvalidate();
17131721
throw CompilerDirectives.shouldNotReachHere("reference was freed: " + Long.toHexString(pointer));
17141722
}
17151723
PythonNativeWrapper wrapper = reference.get();
1716-
if (wrapper == null) {
1717-
CompilerDirectives.transferToInterpreterAndInvalidate();
1718-
throw CompilerDirectives.shouldNotReachHere("reference was collected: " + Long.toHexString(pointer));
1719-
}
1720-
return wrapper.getDelegate();
1724+
return wrapper != null ? wrapper.getDelegate() : null;
17211725
} else {
17221726
IdReference<?> lookup = nativeLookupGet(nativeContext, pointer);
17231727
Object referent;

0 commit comments

Comments
 (0)