Skip to content

Commit

Permalink
Do not resume suspension on destruct
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 3, 2023
1 parent c102b62 commit 7e7a775
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/ReadableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public function isReadable(): bool
public function close(): void
{
if (\is_resource($this->resource) && \get_resource_type($this->resource) === 'stream') {
// Error suppression, as resource might already be closed
$meta = \stream_get_meta_data($this->resource);

if (\str_contains($meta["mode"], "+")) {
Expand All @@ -251,6 +250,9 @@ public function close(): void
}
}

$this->suspension?->resume();
$this->suspension = null;

$this->free();
}

Expand Down Expand Up @@ -328,9 +330,6 @@ private function free(): void
$this->readable = false;
$this->resource = null;

$this->suspension?->resume();
$this->suspension = null;

EventLoop::cancel($this->callbackId);

if (!$this->onClose->isComplete()) {
Expand Down
15 changes: 15 additions & 0 deletions test/ResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ public function testThrowsOnPendingRead(): void
async(fn () => $b->read())->await();
}

public function testCloseWithPendingRead(): void
{
/** @noinspection PhpUnusedLocalVariableInspection Required to keep reference */
[$a, $b] = $this->getStreamPair();

$future = async($b->read(...));
async($b->close(...));

self::assertFalse($b->isClosed());

self::assertNull($future->await());

self::assertTrue($b->isClosed());
}

public function testResolveSuccessOnClosedStream(): void
{
[, $b] = $this->getStreamPair();
Expand Down

0 comments on commit 7e7a775

Please # to comment.