Skip to content

Commit d3b0473

Browse files
committed
Put a lock around the container loading to prevent the file being removed in one process while its created in another
1 parent e9556fb commit d3b0473

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/DependencyInjection/Configurator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use function count;
1616
use function error_reporting;
1717
use function explode;
18+
use function fclose;
19+
use function flock;
20+
use function fopen;
1821
use function implode;
1922
use function in_array;
2023
use function is_dir;
@@ -29,6 +32,8 @@
2932
use function trim;
3033
use function unlink;
3134
use const E_USER_DEPRECATED;
35+
use const LOCK_EX;
36+
use const LOCK_UN;
3237
use const PHP_RELEASE_VERSION;
3338
use const PHP_VERSION_ID;
3439

@@ -74,6 +79,15 @@ public function getContainerCacheDirectory(): string
7479
#[Override]
7580
public function loadContainer(): string
7681
{
82+
$directory = $this->getContainerCacheDirectory();
83+
$locked = false;
84+
if ($this->journalContainer && is_dir($directory)) {
85+
$lockFile = $directory . '/container.lock';
86+
$handle = fopen($lockFile, 'c'); // @ is escalated to exception
87+
flock($handle, LOCK_EX);
88+
$locked = true;
89+
}
90+
7791
$loader = new ContainerLoader(
7892
$this->getContainerCacheDirectory(),
7993
$this->staticParameters['debugMode'],
@@ -95,6 +109,10 @@ public function loadContainer(): string
95109

96110
if ($this->journalContainer) {
97111
$this->journal($className);
112+
if ($locked) {
113+
flock($handle, LOCK_UN);
114+
fclose($handle);
115+
}
98116
}
99117

100118
return $className;

0 commit comments

Comments
 (0)