Skip to content

Commit 3e22061

Browse files
committed
fix: sync IAppConfig cache when CA ID is empty
When getCaId() finds an empty CA ID in cache, it now calls clearCache(true) to reload from database before generating a new one. This prevents race conditions in test environments where CLI commands and HTTP requests may execute simultaneously with independent caches. The solution is simpler than using file locks and works transparently in both test and production environments with minimal overhead. Signed-off-by: Vitor Mattos <[email protected]>
1 parent c644aaf commit 3e22061

1 file changed

Lines changed: 2 additions & 39 deletions

File tree

lib/Handler/CertificateEngine/AEngineHandler.php

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -143,47 +143,10 @@ public function readCertificate(string $certificate, string $privateKey): array
143143
public function getCaId(): string {
144144
$caId = $this->caIdentifierService->getCaId();
145145
if (empty($caId)) {
146-
// In debug mode (tests), use file lock and cache clear to prevent race conditions
147-
// between CLI commands and HTTP requests executing simultaneously.
148-
// In production, this is not needed as setup commands run before HTTP requests.
149-
if ($this->config->getSystemValueBool('debug', false)) {
150-
$caId = $this->getCaIdWithLock();
151-
} else {
152-
$caId = $this->caIdentifierService->generateCaId($this->getName());
153-
}
154-
}
155-
return $caId;
156-
}
157-
158-
private function getCaIdWithLock(): string {
159-
$lockFilePath = $this->tempManager->getTempBaseDir() . '/libresign_ca_id.lock';
160-
$lockFile = fopen($lockFilePath, 'c+');
161-
if ($lockFile === false) {
162-
throw new \RuntimeException('Failed to open lock file');
163-
}
164-
165-
try {
166-
// Acquire exclusive lock (blocks until available)
167-
if (!flock($lockFile, LOCK_EX)) {
168-
throw new \RuntimeException('Failed to acquire lock');
169-
}
170-
171-
// Clear IAppConfig cache and reload from database
172-
// This is critical because IAppConfig cache is per-process
173146
$this->appConfig->clearCache(true);
174-
175-
// Check again after cache refresh - another process might have created it
176-
$caId = $this->caIdentifierService->getCaId();
177-
if (empty($caId)) {
178-
$caId = $this->caIdentifierService->generateCaId($this->getName());
179-
}
180-
181-
return $caId;
182-
} finally {
183-
// Release lock and close file
184-
flock($lockFile, LOCK_UN);
185-
fclose($lockFile);
147+
$caId = $this->caIdentifierService->getCaId() ?: $this->caIdentifierService->generateCaId($this->getName());
186148
}
149+
return $caId;
187150
}
188151

189152
#[\Override]

0 commit comments

Comments
 (0)