1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 11:54:54 +01:00

Ignore double close

This commit is contained in:
Niklas Keller 2017-06-21 14:07:49 +02:00
parent d2c1409bf4
commit 8b43826073
5 changed files with 25 additions and 5 deletions

View File

@ -88,7 +88,7 @@ class BlockingHandle implements Handle {
*/
public function close(): Promise {
if ($this->fh === null) {
throw new ClosedException("The file has already been closed");
return new Success;
}
$fh = $this->fh;

View File

@ -180,7 +180,12 @@ class EioHandle implements Handle {
private function onClose($deferred, $result, $req) {
if ($result === -1) {
$error = \eio_get_last_error($req);
$deferred->fail(new StreamException($error));
if ($error === "Bad file descriptor") {
// Handle is already closed, ignore
$deferred->resolve();
} else {
$deferred->fail(new StreamException("Closing the file failed: " . $error));
}
} else {
$deferred->resolve();
}

View File

@ -5,6 +5,7 @@ namespace Amp\File;
use Amp\ByteStream\ClosedException;
use Amp\ByteStream\StreamException;
use Amp\Coroutine;
use Amp\Failure;
use Amp\Parallel\Worker\TaskException;
use Amp\Parallel\Worker\Worker;
use Amp\Parallel\Worker\WorkerException;
@ -73,7 +74,11 @@ class ParallelHandle implements Handle {
* {@inheritdoc}
*/
public function close(): Promise {
$this->open = false;
if (!$this->writable) {
return new Success;
}
$this->writable = false;
if ($this->worker->isRunning()) {
$promise = $this->worker->enqueue(new Internal\FileTask('fclose', [], $this->id));
@ -81,6 +86,7 @@ class ParallelHandle implements Handle {
return $promise;
}
// FIXME: Should that really return new Success instead of an exception?
return new Success;
}

View File

@ -223,6 +223,7 @@ class UvHandle implements Handle {
$this->poll->listen($deferred->promise());
\uv_fs_close($this->loop, $this->fh, function ($fh) use ($deferred) {
// FIXME: Check for errors
$deferred->resolve();
});

View File

@ -42,8 +42,6 @@ abstract class HandleTest extends TestCase {
$path = Fixture::path() . "/write";
/** @var \Amp\File\Handle $handle */
$handle = yield File\open($path, "c+");
$this->assertSame(0, $handle->tell());
yield $handle->write("foo");
yield $handle->close();
$this->expectException(ClosedException::class);
@ -51,6 +49,16 @@ abstract class HandleTest extends TestCase {
});
}
public function testDoubleClose() {
$this->execute(function () {
$path = Fixture::path() . "/write";
/** @var \Amp\File\Handle $handle */
$handle = yield File\open($path, "c+");
yield $handle->close();
$this->assertNull(yield $handle->close());
});
}
public function testWriteAfterEnd() {
$this->execute(function () {
$path = Fixture::path() . "/write";