Skip to content

Commit 051f9a2

Browse files
[HttpKernel] wrap compilation of the container in an opportunistic lock
1 parent 373469b commit 051f9a2

File tree

1 file changed

+63
-27
lines changed

1 file changed

+63
-27
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -501,25 +501,72 @@ protected function initializeContainer()
501501
$class = $this->getContainerClass();
502502
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
503503
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
504-
$oldContainer = null;
505-
if ($fresh = $cache->isFresh()) {
506-
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
507-
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
508-
$fresh = $oldContainer = false;
509-
try {
510-
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
511-
$this->container->set('kernel', $this);
512-
$oldContainer = $this->container;
513-
$fresh = true;
514-
}
515-
} catch (\Throwable $e) {
516-
} finally {
504+
505+
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
506+
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
507+
508+
try {
509+
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath()) && (!$this->debug || $cache->isFresh())) {
510+
$this->container->set('kernel', $this);
517511
error_reporting($errorLevel);
512+
513+
return;
518514
}
515+
} catch (\Throwable $e) {
519516
}
520517

521-
if ($fresh) {
522-
return;
518+
$oldContainer = \is_object($this->container) ? new \ReflectionClass($this->container) : null;
519+
520+
try {
521+
is_dir($cacheDir) ?: mkdir($cacheDir, 0777, true);
522+
523+
if ($lock = fopen($cache->getPath(), 'w')) {
524+
chmod($cache->getPath(), 0666 & ~umask());
525+
flock($lock, LOCK_EX | LOCK_NB, $wouldBlock);
526+
527+
if (!flock($lock, $wouldBlock ? LOCK_SH : LOCK_EX)) {
528+
fclose($lock);
529+
} else {
530+
$cache = new class($cache->getPath(), $this->debug) extends ConfigCache {
531+
public $lock;
532+
533+
public function write($content, array $metadata = null)
534+
{
535+
fwrite($this->lock, $content);
536+
537+
if (null !== $metadata) {
538+
file_put_contents($this->getPath().'.meta', serialize($metadata));
539+
@chmod($this->getPath().'.meta', 0666 & ~umask());
540+
}
541+
542+
if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
543+
opcache_invalidate($this->getPath(), true);
544+
}
545+
546+
$this->__destruct();
547+
}
548+
549+
public function __destruct()
550+
{
551+
if ($this->lock) {
552+
flock($this->lock, LOCK_UN);
553+
fclose($this->lock);
554+
$this->lock = null;
555+
}
556+
}
557+
};
558+
$cache->lock = $lock;
559+
560+
if (\is_object($this->container = include $cache->getPath()) && (!$oldContainer || \get_class($this->container) !== $oldContainer->name)) {
561+
$this->container->set('kernel', $this);
562+
563+
return;
564+
}
565+
}
566+
}
567+
} catch (\Throwable $e) {
568+
} finally {
569+
error_reporting($errorLevel);
523570
}
524571

525572
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
@@ -565,7 +612,7 @@ protected function initializeContainer()
565612
}
566613

567614
try {
568-
$container = null;
615+
$container = $this->container = null;
569616
$container = $this->buildContainer();
570617
$container->compile();
571618
} finally {
@@ -577,17 +624,6 @@ protected function initializeContainer()
577624
}
578625
}
579626

580-
if (null === $oldContainer && file_exists($cache->getPath())) {
581-
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
582-
try {
583-
$oldContainer = include $cache->getPath();
584-
} catch (\Throwable $e) {
585-
} finally {
586-
error_reporting($errorLevel);
587-
}
588-
}
589-
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
590-
591627
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
592628
$this->container = require $cache->getPath();
593629
$this->container->set('kernel', $this);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy