Repository navigation
Description
What happened
When a local variable is passed by reference anywhere in a function, assigning it an array that is held in another variable ($r = $set;) leaks memory on every assignment. The amount grows with the array: 80 B for one element and 336 B for 32. The callee can be empty, and one by-reference call after the loop is enough. Assigning a fresh literal ($r = [$i];) does not leak, and neither does passing the variable by value. PHP keeps nothing in all cases.
The usual stream_select() loop has exactly this shape ($read = $clients; stream_select($read, $write, $except, …)). stream_select() also leaks by itself: 101 B per call when the caller does not reassign $read.
Our lab loop server (one process, 32 keep-alive clients multiplexed with stream_select()) went from 5 MiB to about 330 MiB RSS in 10 seconds at 64 000 requests/s, all of it anonymous memory. Http\Server does not use stream_select() and stays flat (8–12 MiB) under the same load.
Steps to reproduce
<?php
// refleak.php: RSS growth per loop iteration (200 000 iterations per case)
function rss(): int { $s = (string) file_get_contents('/proc/self/status'); $m = []; return preg_match('/VmRSS:\s+(\d+)/', $s, $m) === 1 ? (int) $m[1] : -1; }
function byRef(?array &$a): void {}
function byVal(?array $a): void {}
function setInCallee(?array &$a): void { $n = $a; $a = $n; }
function report(string $label, int $r0): void { echo str_pad($label . ':', 48), round((rss() - $r0) * 1024 / 200000), " B\n"; }
/** @param resource[] $set */
function plain(array $set): void { $r0 = rss(); for ($i = 0; $i < 200000; $i++) { $r = $set; byVal($r); } report('$r = $set; byVal($r)', $r0); }
/** @param resource[] $set */
function ref(array $set): void { $r0 = rss(); for ($i = 0; $i < 200000; $i++) { $r = $set; byRef($r); } report('$r = $set; byRef($r)', $r0); }
/** @param resource[] $set */
function refAfter(array $set): void { $r0 = rss(); for ($i = 0; $i < 200000; $i++) { $r = $set; } report('$r = $set; (byRef($r) once after the loop)', $r0); byRef($r); }
function fresh(): void { $r0 = rss(); for ($i = 0; $i < 200000; $i++) { $r = [$i]; byRef($r); } report('$r = [$i]; byRef($r)', $r0); }
/** @param resource[] $set */
function callee(array $set): void { $r = $set; $r0 = rss(); for ($i = 0; $i < 200000; $i++) { setInCallee($r); } report('setInCallee($r) without reassigning $r', $r0); }
/** @param resource[] $set */
function sel(array $set): void { $r0 = rss(); for ($i = 0; $i < 200000; $i++) { $r = $set; $w = null; $e = null; stream_select($r, $w, $e, 0); } report('$r = $set; stream_select($r, ...)', $r0); }
/** @param resource[] $set */
function selOnce(array $set): void { $r = $set; $w = null; $e = null; $r0 = rss(); for ($i = 0; $i < 200000; $i++) { stream_select($r, $w, $e, 0); } report('stream_select($r, ...) without reassigning $r', $r0); }
[$a, $b] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($a, 'x'); // $b stays readable, so stream_select() keeps it in $r
$big = [];
for ($j = 0; $j < 32; $j++) { $big[] = $b; }
plain([$b]); ref([$b]); refAfter([$b]); fresh(); callee([$b]); sel([$b]); selOnce([$b]);
echo "32 elements:\n";
ref($big); sel($big);
$ manticore compile refleak.php -o refleak && ./refleak
$r = $set; byVal($r): 6 B
$r = $set; byRef($r): 80 B
$r = $set; (byRef($r) once after the loop): 80 B
$r = [$i]; byRef($r): 0 B
setInCallee($r) without reassigning $r: 0 B
$r = $set; stream_select($r, ...): 80 B
stream_select($r, ...) without reassigning $r: 101 B
32 elements:
$r = $set; byRef($r): 336 B
$r = $set; stream_select($r, ...): 336 B
$ php -n refleak.php
$r = $set; byVal($r): 2 B
(0 B for every other line)
The numbers are the same on two runs and with a compiler built from main at 3b44bdea.
stream_select() reassigns $read itself ($read = $nr; in src/Runtime/Stdlib/Net.php). A user function of the same shape ($nr = []; foreach ($read as $s) { $nr[] = $s; } $read = $nr;, called without reassigning) stays at the noise level of the by-value case. So we could not tell whether the 101 B of the last single-element case have the same cause.
Other operations of our server loop (fread/fwrite on the socket pair, stream_set_blocking(), (int) of a resource, substr/strpos/explode, preg_match() with captures, array_values()) stay within a few KiB over 200 000 calls.
Expected
No growth, as in PHP.
Environment
Manticore 0.17.0 (release tarball manticore-0.17.0-linux-amd64, checksum verified) and main at 3b44bdea built with bin/build; clang 19.1.7; Debian 13, Linux 6.12, x86-64; PHP 8.2.34 for comparison.
Activity