From 5fffe2bb6ac320e1d96b1dec7ab5a50d62709d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 29 Jun 2023 09:51:00 +0200 Subject: [PATCH] Update unwrapped stream to avoid unhandled promise rejections --- src/UnwrapReadableStream.php | 9 +++++++-- tests/FirstTest.php | 11 ----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/UnwrapReadableStream.php b/src/UnwrapReadableStream.php index 536ac92..a0d256f 100644 --- a/src/UnwrapReadableStream.php +++ b/src/UnwrapReadableStream.php @@ -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; @@ -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; } ); } diff --git a/tests/FirstTest.php b/tests/FirstTest.php index c40e64f..b5be03b 100644 --- a/tests/FirstTest.php +++ b/tests/FirstTest.php @@ -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();