Skip to content

Commit d29f15c

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fixed bug #81104
2 parents 3ee6a4b + d8165c2 commit d29f15c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Zend/tests/bug81104.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #81104: Warning: "Failed to set memory limit to ... bytes" emitted after exit in debug
3+
--INI--
4+
memory_limit=5M
5+
report_memleaks=0
6+
--FILE--
7+
<?php
8+
class X {
9+
public $x;
10+
public function __construct() { $this->x = [$this]; }
11+
}
12+
gc_disable();
13+
ini_set('memory_limit', '10M');
14+
$y = [];
15+
for ($i = 0; $i < 10000; $i++) {
16+
$y[] = new X();
17+
}
18+
$y[0]->y = &$y;
19+
20+
?>
21+
===DONE===
22+
--EXPECT--
23+
===DONE===

main/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,14 @@ static PHP_INI_MH(OnChangeMemoryLimit)
272272
value = Z_L(1)<<30; /* effectively, no limit */
273273
}
274274
if (zend_set_memory_limit_ex(value) == FAILURE) {
275-
zend_error(E_WARNING, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
276-
return FAILURE;
275+
/* When the memory limit is reset to the original level during deactivation, we may be
276+
* using more memory than the original limit while shutdown is still in progress.
277+
* Ignore a failure for now, and set the memory limit when the memory manager has been
278+
* shut down and the minimal amount of memory is used. */
279+
if (stage != ZEND_INI_STAGE_DEACTIVATE) {
280+
zend_error(E_WARNING, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true));
281+
return FAILURE;
282+
}
277283
}
278284
PG(memory_limit) = value;
279285
return SUCCESS;
@@ -1849,6 +1855,10 @@ void php_request_shutdown(void *dummy)
18491855
shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0);
18501856
} zend_end_try();
18511857

1858+
/* Reset memory limit, as the reset during INI_STAGE_DEACTIVATE may have failed.
1859+
* At this point, no memory beyond a single chunk should be in use. */
1860+
zend_set_memory_limit(PG(memory_limit));
1861+
18521862
/* 16. Deactivate Zend signals */
18531863
#ifdef ZEND_SIGNALS
18541864
zend_signal_deactivate();

0 commit comments

Comments
 (0)