Skip to content

Commit 6496fa9

Browse files
staabmclxmstaab
andauthored
prevent race conditions while writing cache file (#406)
Co-authored-by: Markus Staab <[email protected]>
1 parent 5033cd6 commit 6496fa9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/QueryReflection/ReflectionCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private function lazyReadRecords()
127127
private function readCachedRecords(bool $useReadLock): ?array
128128
{
129129
if (!is_file($this->cacheFile)) {
130-
if (false === file_put_contents($this->cacheFile, '')) {
130+
if (false === file_put_contents($this->cacheFile, '', LOCK_EX)) {
131131
throw new DbaException(sprintf('Cache file "%s" is not readable and creating a new one did not succeed.', $this->cacheFile));
132132
}
133133
}
@@ -202,7 +202,7 @@ public function persist(): void
202202
'runtimeConfig' => QueryReflection::getRuntimeConfiguration()->toArray(),
203203
], true).';';
204204

205-
if (false === file_put_contents($this->cacheFile, $cacheContent)) {
205+
if (false === file_put_contents($this->cacheFile, $cacheContent, LOCK_EX)) {
206206
throw new DbaException(sprintf('Unable to write cache file "%s"', $this->cacheFile));
207207
}
208208

0 commit comments

Comments
 (0)