Skip to content

Commit 22c9e4a

Browse files
staabmclxmstaab
andauthored
use separate lock-file to prevent race conditions (#146)
Co-authored-by: Markus Staab <[email protected]>
1 parent 5e7870e commit 22c9e4a

File tree

2 files changed

+11
-66
lines changed

2 files changed

+11
-66
lines changed

.phpunit-phpstan-dba.cache

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,6 @@
22
'schemaVersion' => 'v3-rename-props',
33
'records' =>
44
array (
5-
'
6-
SELECT email adaid
7-
WHERE gesperrt = \'1\' AND email LIKE \'%@example.com\'
8-
FROM ada
9-
LIMIT 1
10-
' =>
11-
array (
12-
'error' =>
13-
staabm\PHPStanDba\Error::__set_state(array(
14-
'message' => 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near \'FROM ada LIMIT 0\' at line 3',
15-
'code' => 1064,
16-
)),
17-
),
18-
'
19-
SELECT email, adaid
20-
FROM ada
21-
WHERE gesperrt = \'1\'
22-
LIMIT \'1\'
23-
' =>
24-
array (
25-
'error' => NULL,
26-
),
27-
'
28-
SELECT email, adaid
29-
FROM ada
30-
WHERE gesperrt = \'1\'
31-
LIMIT \'1\', \'1\'
32-
' =>
33-
array (
34-
'error' => NULL,
35-
),
36-
'
37-
SELECT email, adaid
38-
FROM ada
39-
WHERE gesperrt = \'1\'
40-
LIMIT \'1\', \'1\'
41-
' =>
42-
array (
43-
'error' => NULL,
44-
),
45-
'
46-
SELECT email, adaid
47-
FROM ada
48-
WHERE gesperrt = \'1\' AND email LIKE \'%@example%\'
49-
LIMIT 1
50-
' =>
51-
array (
52-
'error' => NULL,
53-
),
54-
'
55-
SELECT email, adaid
56-
FROM ada
57-
WHERE gesperrt = \'1\' AND email LIKE NULL
58-
LIMIT 1
59-
' =>
60-
array (
61-
'error' => NULL,
62-
),
635
'
646
SELECT email adaid
657
WHERE gesperrt = \'1\' AND email LIKE \'%@example.com\'
@@ -2775,11 +2717,6 @@ Simulated query: SELECT email, adaid FROM ada . WHERE email=\'my_other_table\' L
27752717
array (
27762718
'error' => NULL,
27772719
),
2778-
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
2779-
WHERE (gesperrt=\'1\' AND freigabe1u1=1) OR (gesperrt=\'1\' AND freigabe1u1=0)' =>
2780-
array (
2781-
'error' => NULL,
2782-
),
27832720
'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email=\'my_other_table\' LIMIT 1' =>
27842721
array (
27852722
'error' => NULL,

src/QueryReflection/ReflectionCache.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,22 @@ public function persist(): void
7474
return;
7575
}
7676

77+
// prevent parallel phpstan-worker-process from writing into the cache file at the same time
78+
// XXX we use a single system-wide lock file, which might get problematic if multiple users run phpstan on the same machine at the same time
79+
$lockFile = sys_get_temp_dir().'/staabm-phpstan-dba-cache.lock';
80+
$lockHandle = fopen($lockFile, 'w+');
81+
if (false === $lockHandle) {
82+
throw new DbaException(sprintf('Could not open cache file "%s" for writing', $this->cacheFile));
83+
}
84+
flock($lockHandle, LOCK_EX);
85+
7786
// freshly read the cache as it might have changed in the meantime
7887
$cachedRecords = $this->readCache();
7988

80-
// actually we should lock even earlier, but we could no longer read the cache-file with require()
8189
$handle = fopen($this->cacheFile, 'w+');
8290
if (false === $handle) {
8391
throw new DbaException(sprintf('Could not open cache file "%s" for writing', $this->cacheFile));
8492
}
85-
flock($handle, LOCK_EX);
8693

8794
// re-apply all changes to the current cache-state
8895
if (null === $cachedRecords) {
@@ -105,8 +112,9 @@ public function persist(): void
105112
throw new DbaException(sprintf('Unable to write cache file "%s"', $this->cacheFile));
106113
}
107114

108-
// will free the lock implictly
109115
fclose($handle);
116+
// will free the lock implictly
117+
fclose($lockHandle);
110118
}
111119

112120
public function hasValidationError(string $queryString): bool

0 commit comments

Comments
 (0)