Skip to content

Commit

Permalink
Merge pull request #37 from clue-labs/unhandled-rejections
Browse files Browse the repository at this point in the history
Update unwrapped stream to avoid unhandled promise rejections
  • Loading branch information
WyriHaximus authored Jul 7, 2023
2 parents a2e9560 + 5fffe2b commit 4840945
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/UnwrapReadableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use React\Promise\PromiseInterface;
use React\Stream\ReadableStreamInterface;
use React\Stream\ThroughStream;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;

Expand Down Expand Up @@ -75,13 +76,17 @@ function (ReadableStreamInterface $stream) use ($out, &$closed) {
return $stream;
},
function ($e) use ($out, &$closed) {
// Forward exception as error event if not already closed
if (!$closed) {
$out->emit('error', array($e, $out));
$out->close();
}

// resume() and pause() may attach to this promise, so ensure we actually reject here
throw $e;
// Both resume() and pause() may attach to this promise, so
// return a NOOP stream instance here.
$stream = new ThroughStream();
$stream->close();
return $stream;
}
);
}
Expand Down
11 changes: 0 additions & 11 deletions tests/FirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,6 @@ public function testCancelPendingStreamWillReject()
$this->expectPromiseReject($promise);
}

public function testNoGarbageCollectionCyclesAfterClosingStream()
{
\gc_collect_cycles();
$stream = new ThroughStream();
$promise = Stream\first($stream);

$stream->close();

$this->assertSame(0, \gc_collect_cycles());
}

public function testShouldResolveWithoutCreatingGarbageCyclesAfterDataThenClose()
{
\gc_collect_cycles();
Expand Down

0 comments on commit 4840945

Please sign in to comment.