Skip to content

A free, self-hostable news aggregator…

Fix actualize mutex collision across instances (#9045)

Gerard Alvear Porrasc15171efb549
diff --git a/app/actualize_script.php b/app/actualize_script.php
index 252185087..c31e362fb 100755
--- a/app/actualize_script.php
+++ b/app/actualize_script.php
@@ -38,7 +38,7 @@ function notice(string $message): void {
// <Mutex>
// Avoid having multiple actualization processes at the same time
-$mutexFile = TMP_PATH . '/actualize.freshrss.lock';
+$mutexFile = actualize_mutex_file(TMP_PATH, DATA_PATH);
$mutexTtl = 900; // seconds (refreshed before each new feed)
if (file_exists($mutexFile) && ((time() - (@filemtime($mutexFile) ?: 0)) > $mutexTtl)) {
unlink($mutexFile);
diff --git a/lib/lib_rss.php b/lib/lib_rss.php
index afe4d7681..0c767c9fb 100644
--- a/lib/lib_rss.php
+++ b/lib/lib_rss.php
@@ -37,6 +37,16 @@ function join_path(...$path_parts): string {
return join(DIRECTORY_SEPARATOR, $path_parts);
}
+/**
+ * Build the mutex path for an actualisation run.
+ *
+ * The data path identifies a FreshRSS instance, while the temporary path only
+ * determines where its mutex is stored.
+ */
+function actualize_mutex_file(string $tmpPath, string $dataPath): string {
+ return $tmpPath . '/actualize.' . hash('sha256', realpath($dataPath) ?: $dataPath) . '.freshrss.lock';
+}
+
//<Auto-loading>
function classAutoloader(string $class): void {
if (str_starts_with($class, 'FreshRSS')) {
diff --git a/tests/lib/ActualizeMutexTest.php b/tests/lib/ActualizeMutexTest.php
new file mode 100644
index 000000000..7c3e58ad4
--- /dev/null
+++ b/tests/lib/ActualizeMutexTest.php
@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+final class ActualizeMutexTest extends \PHPUnit\Framework\TestCase {
+ public function testMutexFilesAreUniquePerDataPathAndUseTheConfiguredTemporaryPath(): void {
+ $testPath = sys_get_temp_dir() . '/freshrss-actualize-mutex-' . bin2hex(random_bytes(8));
+ $tmpPath = $testPath . '/tmp';
+ $firstDataPath = $testPath . '/first/data';
+ $secondDataPath = $testPath . '/second/data';
+ mkdir($tmpPath, 0700, true);
+ mkdir($firstDataPath, 0700, true);
+ mkdir($secondDataPath, 0700, true);
+
+ try {
+ $firstMutex = actualize_mutex_file($tmpPath, $firstDataPath);
+ $secondMutex = actualize_mutex_file($tmpPath, $secondDataPath);
+
+ self::assertSame($firstMutex, actualize_mutex_file($tmpPath, $firstDataPath . '/.'));
+ self::assertNotSame($firstMutex, $secondMutex);
+ self::assertStringStartsWith($tmpPath . '/actualize.', $firstMutex);
+ self::assertStringEndsWith('.freshrss.lock', $firstMutex);
+
+ $firstHandle = fopen($firstMutex, 'x');
+ $secondHandle = fopen($secondMutex, 'x');
+ self::assertIsResource($firstHandle);
+ self::assertIsResource($secondHandle);
+ fclose($firstHandle);
+ fclose($secondHandle);
+
+ self::assertFalse(@fopen($firstMutex, 'x'));
+ } finally {
+ recursive_unlink($testPath);
+ }
+ }
+}