Skip to content

Commit

Permalink
Fix GH-9266: GC root buffer keeps growing when dtors are present
Browse files Browse the repository at this point in the history
Do not reset cleared count on GC rerun.

Closes GH-9265.
  • Loading branch information
olsavmic authored and cmb69 committed Aug 9, 2022
1 parent c9fa98a commit 0709578
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -5,6 +5,8 @@ PHP NEWS
- Core:
. Fixed --CGI-- support of run-tests.php. (cmb)
. Fixed incorrect double to long casting in latest clang. (zeriyoshi)
. Fixed bug GH-9266 (GC root buffer keeps growing when dtors are present).
(Michael Olšavský)

- Date:
. Fixed bug GH-8730 (DateTime::diff miscalculation is same time zone of
Expand Down
58 changes: 58 additions & 0 deletions Zend/tests/gc_045.phpt
@@ -0,0 +1,58 @@
--TEST--
GC 045: Total count persisted when GC is rerun due to destructor call
--INI--
zend.enable_gc=1
--FILE--
<?php
class GlobalData
{
public static Bar $bar;
}

class Value
{
public function __destruct()
{
new Bar();
}
}

class Bar
{
public function __construct()
{
GlobalData::$bar = $this;
}
}

class Foo
{
public Foo $selfRef;
public Value $val;

public function __construct(Value $val)
{
$this->val = $val;
$this->selfRef = $this;
}
}

for ($j = 0; $j < 10; $j++) {
for ($i = 0; $i < 3000; $i++) {
new Foo(new Value());
}
}

var_dump(gc_status());
?>
--EXPECT--
array(4) {
["runs"]=>
int(10)
["collected"]=>
int(25000)
["threshold"]=>
int(10001)
["roots"]=>
int(10000)
}
6 changes: 4 additions & 2 deletions Zend/zend_gc.c
Expand Up @@ -1439,12 +1439,13 @@ static void zend_gc_root_tmpvars(void);

ZEND_API int zend_gc_collect_cycles(void)
{
int count = 0;
int total_count = 0;
bool should_rerun_gc = 0;
bool did_rerun_gc = 0;

rerun_gc:
if (GC_G(num_roots)) {
int count;
gc_root_buffer *current, *last;
zend_refcounted *p;
uint32_t gc_flags = 0;
Expand Down Expand Up @@ -1622,6 +1623,7 @@ ZEND_API int zend_gc_collect_cycles(void)

GC_TRACE("Collection finished");
GC_G(collected) += count;
total_count += count;
GC_G(gc_active) = 0;
}

Expand All @@ -1638,7 +1640,7 @@ ZEND_API int zend_gc_collect_cycles(void)
finish:
zend_get_gc_buffer_release();
zend_gc_root_tmpvars();
return count;
return total_count;
}

ZEND_API void zend_gc_get_status(zend_gc_status *status)
Expand Down

0 comments on commit 0709578

Please sign in to comment.