Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/Driver/ParallelFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/ParallelFilesystemDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
14 changes: 13 additions & 1 deletion src/Internal/FileWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/Driver/ParallelFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
Loading