Skip to content

Commit b343909

Browse files
committed
Adjust GC threshold if num_roots higher than threshold after collection
This fixes an edge causing the GC to be triggered repeatedly. Destructors might add potential garbage to the buffer, so it may happen that num_root it higher than threshold after collection, thus triggering a GC run almost immediately. This can happen by touching enough objects in a destructor, e.g. by iterating over an array. If the happens again in the new run, and the threshold is not updated, the GC may be triggered repeatedly.
1 parent df72526 commit b343909

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

Zend/tests/gh13670_001.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
GH-13670 001
3+
--FILE--
4+
<?php
5+
6+
register_shutdown_function(function () {
7+
global $shutdown;
8+
$shutdown = true;
9+
});
10+
11+
class Cycle {
12+
public $self;
13+
public function __construct() {
14+
$this->self = $this;
15+
}
16+
public function __destruct() {
17+
global $shutdown;
18+
if (!$shutdown) {
19+
new Cycle();
20+
}
21+
}
22+
}
23+
24+
for ($i = 0; $i < 10001; $i++) {
25+
new Cycle();
26+
}
27+
28+
$objs = [];
29+
for ($i = 0; $i < 100; $i++) {
30+
$obj = new stdClass;
31+
$objs[] = $obj;
32+
}
33+
34+
$st = gc_status();
35+
36+
if ($st['runs'] > 10) {
37+
var_dump($st);
38+
}
39+
?>
40+
==DONE==
41+
--EXPECT--
42+
==DONE==

Zend/tests/gh13670_002.phpt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
GH-13670 002
3+
--FILE--
4+
<?php
5+
6+
register_shutdown_function(function () {
7+
global $shutdown;
8+
$shutdown = true;
9+
});
10+
11+
class Cycle {
12+
public $self;
13+
public function __construct() {
14+
$this->self = $this;
15+
}
16+
}
17+
18+
class Canary {
19+
public $self;
20+
public function __construct() {
21+
$this->self = $this;
22+
}
23+
public function __destruct() {
24+
global $shutdown;
25+
if (!$shutdown) {
26+
work();
27+
}
28+
}
29+
}
30+
31+
function work() {
32+
global $objs;
33+
new Canary();
34+
// Create some collectable garbage so the next run will not adjust
35+
// threshold
36+
for ($i = 0; $i < 100; $i++) {
37+
new Cycle();
38+
}
39+
// Add potential garbage to buffer
40+
foreach ($objs as $obj) {
41+
$o = $obj;
42+
}
43+
}
44+
45+
$objs = [];
46+
for ($i = 0; $i < 10000; $i++) {
47+
$obj = new stdClass;
48+
$objs[] = $obj;
49+
}
50+
51+
work();
52+
53+
$st = gc_status();
54+
55+
if ($st['runs'] > 10) {
56+
var_dump($st);
57+
}
58+
?>
59+
==DONE==
60+
--EXPECT--
61+
==DONE==

Zend/zend_gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static void gc_adjust_threshold(int count)
608608
/* TODO Very simple heuristic for dynamic GC buffer resizing:
609609
* If there are "too few" collections, increase the collection threshold
610610
* by a fixed step */
611-
if (count < GC_THRESHOLD_TRIGGER) {
611+
if (count < GC_THRESHOLD_TRIGGER || GC_G(num_roots) >= GC_G(gc_threshold)) {
612612
/* increase */
613613
if (GC_G(gc_threshold) < GC_THRESHOLD_MAX) {
614614
new_threshold = GC_G(gc_threshold) + GC_THRESHOLD_STEP;

0 commit comments

Comments
 (0)