Skip to content

Commit 89c87af

Browse files
committed
return null for collected and freed items during collection
style fix
1 parent 47a93ca commit 89c87af

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GcModuleBuiltins.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ static long javaCollect(Node inliningTarget, GilNode gil) {
180180
Runtime runtime = Runtime.getRuntime();
181181
long freeMemory = runtime.freeMemory();
182182
try {
183-
CApiTransitions.ensurePollRefQueueCleanup();
183+
PythonUtils.forceFullGC();
184+
try {
185+
Thread.sleep(15);
186+
} catch (InterruptedException e) {
187+
// doesn't matter, just trying to give the GC more time
188+
}
184189
} finally {
185190
gil.acquire();
186191
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import static com.oracle.graal.python.PythonLanguage.CONTEXT_INSENSITIVE_SINGLETONS;
4444
import static com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper.PythonAbstractObjectNativeWrapper.IMMORTAL_REFCNT;
45+
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.pollReferenceQueue;
4546
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___FILE__;
4647
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___LIBRARY__;
4748
import static com.oracle.graal.python.nodes.StringLiterals.J_LLVM_LANGUAGE;
@@ -713,7 +714,7 @@ public void triggerGC(PythonContext context, long size, Node caller) {
713714
for (int retries = 0; retries < MAX_COLLECTION_RETRIES; retries++) {
714715
delay += 50;
715716
doGc(delay);
716-
CApiTransitions.pollReferenceQueue();
717+
pollReferenceQueue();
717718
PythonContext.triggerAsyncActions(caller);
718719
if (allocatedMemory + size <= context.getOption(PythonOptions.MaxNativeMemory)) {
719720
allocatedMemory += size;
@@ -958,12 +959,12 @@ public void exitCApiContext() {
958959
* deallocating objects may run arbitrary guest code that can again call into the
959960
* interpreter.
960961
*/
961-
CApiTransitions.ensurePollRefQueueCleanup();
962+
pollReferenceQueue();
962963
PythonThreadState threadState = getContext().getThreadState(getContext().getLanguage());
963964
Object nativeThreadState = PThreadState.getNativeThreadState(threadState);
964965
if (nativeThreadState != null) {
965966
PCallCapiFunction.callUncached(NativeCAPISymbol.FUN_PY_GC_COLLECT_NO_FAIL, nativeThreadState);
966-
CApiTransitions.pollReferenceQueue();
967+
pollReferenceQueue();
967968
}
968969
CApiTransitions.deallocateNativeWeakRefs(getContext());
969970
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
import com.oracle.graal.python.runtime.GilNode;
104104
import com.oracle.graal.python.runtime.PythonContext;
105105
import com.oracle.graal.python.runtime.PythonOptions;
106-
import com.oracle.graal.python.runtime.exception.PException;
107106
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
108107
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
109108
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
@@ -1506,11 +1505,19 @@ Object doNonWrapper(Object value,
15061505
int idx = readI32Node.read(HandlePointerConverter.pointerToStub(pointer), CFields.GraalPyObject__handle_table_index);
15071506
PythonObjectReference reference = nativeStubLookupGet(nativeContext, pointer, idx);
15081507
if (reference == null) {
1508+
int collecting = readI32Node.read(pythonContext.getCApiContext().getGCState(), CFields.GCState__collecting);
1509+
if (collecting == 1) {
1510+
return PNone.NO_VALUE;
1511+
}
15091512
CompilerDirectives.transferToInterpreterAndInvalidate();
15101513
throw CompilerDirectives.shouldNotReachHere("reference was freed: " + Long.toHexString(pointer));
15111514
}
15121515
wrapper = reference.get();
15131516
if (wrapper == null) {
1517+
int collecting = readI32Node.read(pythonContext.getCApiContext().getGCState(), CFields.GCState__collecting);
1518+
if (collecting == 1) {
1519+
return PNone.NO_VALUE;
1520+
}
15141521
CompilerDirectives.transferToInterpreterAndInvalidate();
15151522
throw CompilerDirectives.shouldNotReachHere("reference was collected: " + Long.toHexString(pointer));
15161523
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/structs/CFields.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public enum CFields {
345345
GCState__debug(Int),
346346
GCState__generations(Pointer),
347347
GCState__generation0(Pointer),
348+
GCState__collecting(Int),
348349

349350
GCGeneration__count(Int),
350351

0 commit comments

Comments
 (0)