Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/file_cache_storage_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: File Cache Storage Test

on:
pull_request: null
push:
branches:
- main



env:
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
COMPOSER_ROOT_VERSION: "dev-main"

jobs:
tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
php-versions: ['8.4']

runs-on: ${{ matrix.os }}
timeout-minutes: 3

name: File Cache Storage ${{ matrix.php-versions }} (${{ matrix.os }})
steps:
- uses: actions/checkout@v5

-
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
ini-values: zend.assertions=1

- uses: "ramsey/composer-install@v4"

- run: vendor/bin/phpunit tests/Caching/ValueObject/Storage/FileCacheStorageTest.php --colors
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"doctrine/inflector": "^2.1",
"entropy/entropy": "^0.4.12",
"fidry/cpu-core-counter": "^1.1",
"nette/utils": "^4.1.4",
"nette/utils": "^4.1.5",
"nikic/php-parser": "^5.9",
"ondram/ci-detector": "^4.2",
"phpstan/phpdoc-parser": "^2.3.3",
Expand Down
17 changes: 3 additions & 14 deletions src/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use FilesystemIterator;
use Nette\Utils\FileSystem;
use Nette\Utils\Random;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Caching\ValueObject\CacheFilePaths;
use Rector\Caching\ValueObject\CacheItem;
Expand Down Expand Up @@ -55,7 +54,6 @@ public function save(string $key, string $variableKey, mixed $data): void

$filePath = $cacheFilePaths->getFilePath();

$tmpPath = \sprintf('%s/%s.tmp', $this->directory, Random::generate());
$errorBefore = \error_get_last();
$exported = @\var_export(new CacheItem($variableKey, $data), true);
$errorAfter = \error_get_last();
Expand All @@ -68,18 +66,9 @@ public function save(string $key, string $variableKey, mixed $data): void
));
}

// for performance reasons we don't use SmartFileSystem
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
$copySuccess = @\copy($tmpPath, $filePath);
@\unlink($tmpPath);

if ($copySuccess) {
return;
}

if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
// atomic write via temp file + rename(), so a parallel reader never sees a partially written cache file
// handles the Windows rename() edge case too, see https://github.com/rectorphp/rector/issues/9876
FileSystem::writeAtomic($filePath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
}

public function clean(string $key): void
Expand Down
29 changes: 29 additions & 0 deletions tests/Caching/ValueObject/Storage/FileCacheStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ public function testClean(): void
$this->assertDirectoryDoesNotExist(__DIR__ . '/Source/0e');
}

public function testSaveLeavesConcurrentReaderOnCompleteFile(): void
{
if (\DIRECTORY_SEPARATOR === '\\') {
// Windows blocks rename() over a file open in another handle, so the atomic-replace-under-open-reader
// scenario this asserts is POSIX-only; on Windows writeAtomic() retries the transient lock instead
$this->markTestSkipped('Atomic replace under an open reader is POSIX-only');
}

$filePath = __DIR__ . '/Source/0e/76/0e76658526655756207688271159624026011393.php';

$this->fileCacheStorage->save('aaK1STfY', 'TEST', 'first');
$contentsBeforeSave = (string) file_get_contents($filePath);

// every parallel worker require()s this path while booting; open it as such a worker would,
// then save over it mid-read - an atomic write must leave the reader on the file it opened
$readerHandle = fopen($filePath, 'r');
$this->assertNotFalse($readerHandle);

$this->fileCacheStorage->save('aaK1STfY', 'TEST', 'second');

$contentsSeenByReader = stream_get_contents($readerHandle);
fclose($readerHandle);

$this->assertSame($contentsBeforeSave, $contentsSeenByReader);
$this->assertSame('second', $this->fileCacheStorage->load('aaK1STfY', 'TEST'));

$this->fileCacheStorage->clean('aaK1STfY');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config.php';
Expand Down
Loading