Skip to content

Rerun GC if destructors encountered #5581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Zend/tests/gc_011.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ $a->a = $a;
var_dump($a);
unset($a);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
echo "ok\n"
?>
--EXPECTF--
Expand All @@ -24,6 +23,5 @@ object(Foo)#%d (1) {
*RECURSION*
}
__destruct
int(0)
int(1)
ok
2 changes: 0 additions & 2 deletions Zend/tests/gc_016.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ $a = new Foo();
$a->a = $a;
unset($a);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
echo "ok\n"
?>
--EXPECT--
-> int(0)
int(0)
int(2)
ok
2 changes: 0 additions & 2 deletions Zend/tests/gc_017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ unset($a);
unset($b);
unset($c);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
echo "ok\n"
?>
--EXPECTF--
string(1) "%s"
string(1) "%s"
string(1) "%s"
int(0)
int(1)
ok
2 changes: 0 additions & 2 deletions Zend/tests/gc_028.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ $bar->foo = $foo;
unset($foo);
unset($bar);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
?>
--EXPECT--
int(0)
int(1)
2 changes: 0 additions & 2 deletions Zend/tests/gc_029.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ $bar->foo = $foo;
unset($foo);
unset($bar);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
?>
--EXPECT--
int(0)
int(1)
2 changes: 0 additions & 2 deletions Zend/tests/gc_035.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ $a->x[] = $a;
var_dump(gc_collect_cycles());
unset($a);
var_dump(gc_collect_cycles());
var_dump(gc_collect_cycles());
?>
--EXPECT--
int(0)
int(0)
int(2)
16 changes: 14 additions & 2 deletions Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,10 @@ static void zend_get_gc_buffer_release(void);
ZEND_API int zend_gc_collect_cycles(void)
{
int count = 0;
zend_bool should_rerun_gc = 0;
zend_bool did_rerun_gc = 0;

rerun_gc:
if (GC_G(num_roots)) {
gc_root_buffer *current, *last;
zend_refcounted *p;
Expand Down Expand Up @@ -1475,8 +1478,8 @@ ZEND_API int zend_gc_collect_cycles(void)
* be introduced. These references can be introduced in a way that does not
* modify any refcounts, so we have no real way to detect this situation
* short of rerunning full GC tracing. What we do instead is to only run
* destructors at this point, and leave the actual freeing of the objects
* until the next GC run. */
* destructors at this point and automatically re-run GC afterwards. */
should_rerun_gc = 1;

/* Mark all roots for which a dtor will be invoked as DTOR_GARBAGE. Additionally
* color them purple. This serves a double purpose: First, they should be
Expand Down Expand Up @@ -1609,6 +1612,15 @@ ZEND_API int zend_gc_collect_cycles(void)
}

gc_compact();

/* Objects with destructors were removed from this GC run. Rerun GC right away to clean them
* up. We do this only once: If we encounter more destructors on the second run, we'll not
* run GC another time. */
if (should_rerun_gc && !did_rerun_gc) {
did_rerun_gc = 1;
goto rerun_gc;
}

zend_get_gc_buffer_release();
return count;
}
Expand Down