@@ -1684,7 +1684,11 @@ Object doNonWrapper(long pointer, boolean stealing,
1684
1684
* Very similar to {@link NativePtrToPythonNode}, this node resolves a native pointer (given as
1685
1685
* Java {@code long}) to a Python object. However, it will never create a fresh
1686
1686
* {@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}.
1688
1692
*/
1689
1693
@ GenerateUncached
1690
1694
@ GenerateInline
@@ -1709,15 +1713,15 @@ static Object doLong(Node inliningTarget, long pointer,
1709
1713
int idx = readI32Node .read (HandlePointerConverter .pointerToStub (pointer ), CFields .GraalPyObject__handle_table_index );
1710
1714
PythonObjectReference reference = nativeStubLookupGet (nativeContext , pointer , idx );
1711
1715
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
+ */
1712
1720
CompilerDirectives .transferToInterpreterAndInvalidate ();
1713
1721
throw CompilerDirectives .shouldNotReachHere ("reference was freed: " + Long .toHexString (pointer ));
1714
1722
}
1715
1723
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 ;
1721
1725
} else {
1722
1726
IdReference <?> lookup = nativeLookupGet (nativeContext , pointer );
1723
1727
Object referent ;
0 commit comments