Skip to content

Commit

Permalink
bug #36169 [HttpKernel] fix locking for PHP 7.4+ (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[HttpKernel] fix locking for PHP 7.4+

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #36132
| License       | MIT
| Doc PR        | -

As explained in https://bugs.php.net/79398

Commits
-------

f618b98 [HttpKernel] fix locking for PHP 7.4+
  • Loading branch information
nicolas-grekas committed Mar 23, 2020
2 parents 099481f + f618b98 commit 7f5d017
Showing 1 changed file with 14 additions and 42 deletions.
56 changes: 14 additions & 42 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -38,9 +38,6 @@
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;

// Help opcache.preload discover always-needed symbols
class_exists(ConfigCache::class);

/**
* The Kernel is the heart of the Symfony system.
*
Expand Down Expand Up @@ -533,47 +530,20 @@ protected function initializeContainer()
try {
is_dir($cacheDir) ?: mkdir($cacheDir, 0777, true);

if ($lock = fopen($cachePath, 'w')) {
chmod($cachePath, 0666 & ~umask());
if ($lock = fopen($cachePath.'.lock', 'w')) {
flock($lock, LOCK_EX | LOCK_NB, $wouldBlock);

if (!flock($lock, $wouldBlock ? LOCK_SH : LOCK_EX)) {
fclose($lock);
} else {
$cache = new class($cachePath, $this->debug) extends ConfigCache {
public $lock;

public function write($content, array $metadata = null)
{
rewind($this->lock);
ftruncate($this->lock, 0);
fwrite($this->lock, $content);

if (null !== $metadata) {
file_put_contents($this->getPath().'.meta', serialize($metadata));
@chmod($this->getPath().'.meta', 0666 & ~umask());
}

if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
@opcache_invalidate($this->getPath(), true);
}
}

public function release()
{
flock($this->lock, LOCK_UN);
fclose($this->lock);
}
};
$cache->lock = $lock;

if (!\is_object($this->container = include $cachePath)) {
$this->container = null;
} elseif (!$oldContainer || \get_class($this->container) !== $oldContainer->name) {
$this->container->set('kernel', $this);

return;
}
$lock = null;
} elseif (!\is_object($this->container = include $cachePath)) {
$this->container = null;
} elseif (!$oldContainer || \get_class($this->container) !== $oldContainer->name) {
flock($lock, LOCK_UN);
fclose($lock);
$this->container->set('kernel', $this);

return;
}
}
} catch (\Throwable $e) {
Expand Down Expand Up @@ -637,8 +607,10 @@ public function release()
}

$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
if (method_exists($cache, 'release')) {
$cache->release();

if ($lock) {
flock($lock, LOCK_UN);
fclose($lock);
}

$this->container = require $cachePath;
Expand Down

0 comments on commit 7f5d017

Please sign in to comment.