From 5d55b91faebc9690fe58ea213cd291b73fdfd916 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 27 Jun 2019 23:36:22 +0200 Subject: [PATCH] [Cache] work aroung PHP memory leak --- .../Component/Cache/Traits/PhpFilesTrait.php | 57 ++++++++++++++++--- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Cache/Traits/PhpFilesTrait.php b/src/Symfony/Component/Cache/Traits/PhpFilesTrait.php index 6afdd8c3a5..5ed4d6023e 100644 --- a/src/Symfony/Component/Cache/Traits/PhpFilesTrait.php +++ b/src/Symfony/Component/Cache/Traits/PhpFilesTrait.php @@ -50,12 +50,15 @@ trait PhpFilesTrait { $time = time(); $pruned = true; + $getExpiry = true; set_error_handler($this->includeHandler); try { foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { try { - list($expiresAt) = include $file; + if (\is_array($expiresAt = include $file)) { + $expiresAt = $expiresAt[0]; + } } catch (\ErrorException $e) { $expiresAt = $time; } @@ -87,15 +90,21 @@ trait PhpFilesTrait $values = []; begin: + $getExpiry = false; + foreach ($ids as $id) { if (null === $value = $this->values[$id] ?? null) { $missingIds[] = $id; } elseif ('N;' === $value) { $values[$id] = null; - } elseif ($value instanceof \Closure) { - $values[$id] = $value(); - } else { + } elseif (!\is_object($value)) { $values[$id] = $value; + } elseif (!$value instanceof LazyValue) { + // calling a Closure is for @deprecated BC and should be removed in Symfony 5.0 + $values[$id] = $value(); + } elseif (false === $values[$id] = include $value->file) { + unset($values[$id], $this->values[$id]); + $missingIds[] = $id; } if (!$this->appendOnly) { unset($this->values[$id]); @@ -108,10 +117,18 @@ trait PhpFilesTrait set_error_handler($this->includeHandler); try { + $getExpiry = true; + foreach ($missingIds as $k => $id) { try { $file = $this->files[$id] ?? $this->files[$id] = $this->getFile($id); - list($expiresAt, $this->values[$id]) = include $file; + + if (\is_array($expiresAt = include $file)) { + [$expiresAt, $this->values[$id]] = $expiresAt; + } elseif ($now < $expiresAt) { + $this->values[$id] = new LazyValue($file); + } + if ($now >= $expiresAt) { unset($this->values[$id], $missingIds[$k]); } @@ -140,7 +157,13 @@ trait PhpFilesTrait set_error_handler($this->includeHandler); try { $file = $this->files[$id] ?? $this->files[$id] = $this->getFile($id); - list($expiresAt, $value) = include $file; + $getExpiry = true; + + if (\is_array($expiresAt = include $file)) { + [$expiresAt, $value] = $expiresAt; + } elseif ($this->appendOnly) { + $value = new LazyValue($file); + } } catch (\ErrorException $e) { return false; } finally { @@ -189,13 +212,16 @@ trait PhpFilesTrait } if (!$isStaticValue) { - $value = str_replace("\n", "\n ", $value); - $value = "static function () {\n\n return {$value};\n\n}"; + // We cannot use a closure here because of https://bugs.php.net/76982 + $value = str_replace('\Symfony\Component\VarExporter\Internal\\', '', $value); + $value = "files[$key] = $this->getFile($key, true); // Since OPcache only compiles files older than the script execution start, set the file's mtime in the past - $ok = $this->write($file, "write($file, $value, self::$startTime - 10) && $ok; if ($allowCompile) { @opcache_invalidate($file, true); @@ -241,3 +267,16 @@ trait PhpFilesTrait return @unlink($file); } } + +/** + * @internal + */ +class LazyValue +{ + public $file; + + public function __construct($file) + { + $this->file = $file; + } +}