diff --git a/src/Driver/ParallelFile.php b/src/Driver/ParallelFile.php index a0b0562..97dcfae 100644 --- a/src/Driver/ParallelFile.php +++ b/src/Driver/ParallelFile.php @@ -13,6 +13,7 @@ use Amp\File\PendingOperationError; use Amp\File\Whence; use Amp\Future; +use Amp\Parallel\Context\StatusError; use Amp\Parallel\Worker\TaskFailureException; use Amp\Parallel\Worker\WorkerException; use Revolt\EventLoop; @@ -66,7 +67,13 @@ public function __destruct() $id = $this->id; $this->id = null; $worker = $this->worker; - EventLoop::queue(static fn () => $worker->execute(new Internal\FileTask('fclose', [], $id))); + EventLoop::queue(static function () use ($worker, $id): void { + try { + $worker->execute(new Internal\FileTask('fclose', [], $id)); + } catch (StatusError|WorkerException) { + // The worker exited meanwhile (its pool was shut down, or it died), closing the file. + } + }); } if (!$this->onClose->isComplete()) { diff --git a/src/Driver/ParallelFilesystemDriver.php b/src/Driver/ParallelFilesystemDriver.php index 756f3a6..b797b8c 100644 --- a/src/Driver/ParallelFilesystemDriver.php +++ b/src/Driver/ParallelFilesystemDriver.php @@ -48,7 +48,7 @@ public function openFile(string $path, string $mode): ParallelFile if (($workerStorage[$worker] -= 1) === 0 || !$worker->isRunning()) { unset($workerStorage[$worker]); } - }); + }, $this->pool); try { [$id, $size, $mode] = $worker->execute(new Internal\FileTask("fopen", [$path, $mode])); diff --git a/src/Internal/FileWorker.php b/src/Internal/FileWorker.php index 82493f9..e8d1d3a 100644 --- a/src/Internal/FileWorker.php +++ b/src/Internal/FileWorker.php @@ -5,17 +5,29 @@ use Amp\Cancellation; use Amp\Parallel\Worker\Task; use Amp\Parallel\Worker\Worker; +use Amp\Parallel\Worker\WorkerPool; /** @internal */ final class FileWorker { + /** + * The pool of the worker, kept alive while the file is open: a pool created by the driver would otherwise kill + * the worker, closing the file, once the driver is destroyed. + * + * @psalm-suppress UnusedProperty Only kept alive. + */ + private readonly WorkerPool $pool; + /** * @param \Closure(Worker):void $push Closure to push the worker back into the queue. + * @param WorkerPool $pool The pool of the worker. */ public function __construct( private readonly Worker $worker, - private readonly \Closure $push + private readonly \Closure $push, + WorkerPool $pool, ) { + $this->pool = $pool; } /** diff --git a/test/Driver/ParallelFileTest.php b/test/Driver/ParallelFileTest.php index ae648fc..611e300 100644 --- a/test/Driver/ParallelFileTest.php +++ b/test/Driver/ParallelFileTest.php @@ -7,6 +7,7 @@ use Amp\File\Test\AsyncFileTest; use Amp\Parallel\Worker\ContextWorkerPool; use Amp\Parallel\Worker\WorkerPool; +use function Amp\delay; class ParallelFileTest extends AsyncFileTest { @@ -49,4 +50,28 @@ public function testMultipleOpenFiles(int $maxCount) $this->assertSame($maxCount, $this->pool->getWorkerCount()); } + + public function testOpenFileKeepsTheDriversPoolAlive(): void + { + // The driver creates and owns its pool: the file must keep it, and the worker holding the file, alive. + $driver = new ParallelFilesystemDriver(); + $file = $driver->openFile(__FILE__, 'r'); + unset($driver); + \gc_collect_cycles(); + + $this->assertSame(\file_get_contents(__FILE__, length: 5), $file->read(length: 5)); + $file->close(); + } + + public function testFileDestroyedBeforeItsWorkerShutsDown(): void + { + $file = $this->driver->openFile(__FILE__, 'r'); + + // Destroying the file queues its close; the worker exits before the close is sent. + unset($file); + $this->pool->kill(); + + delay(0.1); + $this->assertFalse($this->pool->isRunning()); + } }