Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(io): do not throw on close. #296

Merged
merged 1 commit into from
Dec 2, 2021
Merged
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
107 changes: 72 additions & 35 deletions src/Psl/IO/Internal/ResourceHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ class ResourceHandle implements IO\Stream\CloseSeekReadWriteHandleInterface
*/
private string $writeWatcher = 'invalid';

/**
* @var null|Async\Deferred<null>
*/
private ?Async\Deferred $readDeferred = null;

/**
* @var null|Async\Deferred<null>
*/
private ?Async\Deferred $writeDeferred = null;

/**
Expand Down Expand Up @@ -91,8 +98,10 @@ public function __construct(mixed $stream, bool $read, bool $write, bool $seek)

$deferred = &$this->readDeferred;
$this->readWatcher = Async\Scheduler::onReadable($stream, static function () use (&$deferred) {
/** @var Async\Deferred|null $deferred */
$deferred?->complete(null);
/** @var Async\Deferred<null>|null $tmp */
$tmp = $deferred;
$deferred = null;
$tmp?->complete(null);
});

Async\Scheduler::disable($this->readWatcher);
Expand All @@ -109,8 +118,10 @@ public function __construct(mixed $stream, bool $read, bool $write, bool $seek)

$deferred = &$this->writeDeferred;
$this->writeWatcher = Async\Scheduler::onWritable($stream, static function () use (&$deferred) {
/** @var Async\Deferred|null $deferred */
$deferred?->complete(null);
/** @var Async\Deferred<null>|null $tmp */
$tmp = $deferred;
$deferred = null;
$tmp?->complete(null);
});

Async\Scheduler::disable($this->writeWatcher);
Expand All @@ -131,6 +142,7 @@ public function write(string $bytes, ?float $timeout = null): int

$bytes = substr($bytes, $written);

/** @var Async\Deferred<null> */
$this->writeDeferred = new Async\Deferred();
$deferred = &$this->writeDeferred;
/** @psalm-suppress MissingThrowsDocblock */
Expand All @@ -142,11 +154,15 @@ public function write(string $bytes, ?float $timeout = null): int
$timeout,
static function () use (&$deferred) {
/** @var Async\Deferred|null $deferred */
$deferred?->error(
$tmp = $deferred;
$deferred = null;
$tmp?->error(
new Exception\TimeoutException('reached timeout while the handle is still not writable.')
);
}
);

Async\Scheduler::unreference($delay_watcher);
}

try {
Expand Down Expand Up @@ -235,29 +251,36 @@ public function read(?int $max_bytes = null, ?float $timeout = null): string
return $chunk;
}

/** @var Async\Deferred<null> */
$this->readDeferred = new Async\Deferred();
$deferred = &$this->readDeferred;
/** @psalm-suppress MissingThrowsDocblock */
Async\Scheduler::enable($this->readWatcher);
$delay_watcher = null;
if (null !== $timeout) {
$timeout = $timeout < 0.0 ? 0.0 : $timeout;
$read_watcher = $this->readWatcher;
$delay_watcher = Async\Scheduler::delay(
$timeout,
static function () use (&$deferred) {
/** @var Async\Deferred|null $deferred */
$deferred?->error(
static function () use (&$deferred, $read_watcher) {
Async\Scheduler::disable($read_watcher);

/** @var Async\Deferred|null $tmp */
$tmp = $deferred;
$deferred = null;
$tmp?->error(
new Exception\TimeoutException('reached timeout while the handle is still not readable.')
);
}
);

Async\Scheduler::unreference($delay_watcher);
}

try {
/** @var Async\Deferred $deferred */
$deferred->getAwaitable()->await();
} finally {
$deferred = null;
Async\Scheduler::disable($this->readWatcher);
if (null !== $delay_watcher) {
Async\Scheduler::cancel($delay_watcher);
Expand Down Expand Up @@ -300,32 +323,6 @@ public function readImmediately(?int $max_bytes = null): string
return $result;
}

/**
* {@inheritDoc}
*/
public function close(): void
{
if (is_resource($this->stream)) {
/** @psalm-suppress PossiblyInvalidArgument */
$stream = $this->stream;
$this->stream = null;
$result = @fclose($stream);
if ($result === false) {
/** @var array{message: string} $error */
$error = error_get_last();

throw new Exception\RuntimeException($error['message'] ?? 'unknown error.');
}
} else {
// Stream could be set to a non-null closed-resource,
// if manually closed using `fclose($handle->getStream)`.
$this->stream = null;
}

$this->readDeferred?->error(throw new Exception\AlreadyClosedException('Handle has already been closed.'));
$this->writeDeferred?->error(throw new Exception\AlreadyClosedException('Handle has already been closed.'));
}

/**
* @return object|resource|null
*/
Expand All @@ -338,4 +335,44 @@ public function __destruct()
{
$this->close();
}

/**
* {@inheritDoc}
*/
public function close(): void
{
if (null !== $this->stream) {
Async\Scheduler::disable($this->readWatcher);
Async\Scheduler::disable($this->writeWatcher);

if (is_resource($this->stream)) {
/** @psalm-suppress PossiblyInvalidArgument */
$stream = $this->stream;
$this->stream = null;
$result = @fclose($stream);
if ($result === false) {
/** @var array{message: string} $error */
$error = error_get_last();

throw new Exception\RuntimeException($error['message'] ?? 'unknown error.');
}
} else {
// Stream could be set to a non-null closed-resource,
// if manually closed using `fclose($handle->getStream)`.
$this->stream = null;
}

if ($this->readDeferred) {
$deferred = $this->readDeferred;
$this->readDeferred = null;
$deferred->error(new Exception\AlreadyClosedException('Handle has already been closed.'));
}

if ($this->writeDeferred) {
$deferred = $this->writeDeferred;
$this->readDeferred = null;
$deferred->error(new Exception\AlreadyClosedException('Handle has already been closed.'));
}
}
}
}