Skip to content

Commit f0537fd

Browse files
committed
[GR-61500] Avoid sending dead stubs to PyTruffleObject_ReplicateNativeReferences
PullRequest: graalpython/3652
2 parents 82e5ef7 + 4045cbb commit f0537fd

File tree

1 file changed

+15
-2
lines changed
  • graalpython/com.oracle.graal.python.cext/src

1 file changed

+15
-2
lines changed

graalpython/com.oracle.graal.python.cext/src/gcmodule.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2024, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2024 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -498,7 +498,20 @@ push_native_references_to_managed(PyObject *op, GraalPyGC_Cycle *cycle)
498498

499499
// avoid costly upcalls
500500
if (cycle->n > 0) {
501-
GraalPyTruffleObject_ReplicateNativeReferences(op, cycle->head, cycle->n);
501+
int dead = 0;
502+
// Some managed handles can be already collected if the GC is already cleaning the cycle
503+
for (n = cycle->head; n != NULL; n = n->next) {
504+
if (points_to_py_handle_space(n->item)) {
505+
GraalPyObject* obj = (GraalPyObject*)pointer_to_stub(n->item);
506+
if (obj->handle_table_index == 0) {
507+
dead = 1;
508+
break;
509+
}
510+
}
511+
}
512+
if (!dead) {
513+
GraalPyTruffleObject_ReplicateNativeReferences(op, cycle->head, cycle->n);
514+
}
502515
}
503516

504517
// destroy list

0 commit comments

Comments
 (0)