Skip to content

Commit

Permalink
Merge c703919 into d5cd42a
Browse files Browse the repository at this point in the history
  • Loading branch information
psafarov committed May 6, 2019
2 parents d5cd42a + c703919 commit b55e8bd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
12 changes: 2 additions & 10 deletions lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public function __construct($stream, int $chunkSize = null)
continue;
}

if (!\is_resource($stream) || @\feof($stream)) {
throw new StreamException("The stream was closed by the peer");
}

// Error reporting suppressed since fwrite() emits E_WARNING if the pipe is broken or the buffer is full.
// Use conditional, because PHP doesn't like getting null passed
if ($chunkSize) {
Expand All @@ -86,7 +82,7 @@ public function __construct($stream, int $chunkSize = null)
\assert($written !== false, "Trying to write on a previously fclose()'d resource. Do NOT manually fclose() resources the loop still has a reference to.");

// Broken pipes between processes on macOS/FreeBSD do not detect EOF properly.
if ($written === 0) {
if ($written === 0 || !@\fflush($stream)) {
if ($emptyWrites++ > self::MAX_CONSECUTIVE_EMPTY_WRITES) {
$message = "Failed to write to stream after multiple attempts";
if ($error = \error_get_last()) {
Expand Down Expand Up @@ -179,10 +175,6 @@ private function send(string $data, bool $end = false): Promise
return new Success(0);
}

if (!\is_resource($this->resource) || @\feof($this->resource)) {
return new Failure(new StreamException("The stream was closed by the peer"));
}

// Error reporting suppressed since fwrite() emits E_WARNING if the pipe is broken or the buffer is full.
// Use conditional, because PHP doesn't like getting null passed.
if ($this->chunkSize) {
Expand All @@ -193,7 +185,7 @@ private function send(string $data, bool $end = false): Promise

\assert($written !== false, "Trying to write on a previously fclose()'d resource. Do NOT manually fclose() resources the loop still has a reference to.");

if ($length === $written) {
if ($length === $written && @\fflush($this->resource)) {
if ($end) {
$this->close();
}
Expand Down
6 changes: 3 additions & 3 deletions test/ResourceOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testBrokenPipe()
\fclose($b);

$this->expectException(StreamException::class);
$this->expectExceptionMessage("The stream was closed by the peer");
$this->expectExceptionMessage("Failed to write to stream after multiple attempts");
wait($stream->write("foobar"));
}

Expand All @@ -60,7 +60,7 @@ public function testClosedRemoteSocket()
\fclose($b);

$this->expectException(StreamException::class);
$this->expectExceptionMessage("The stream was closed by the peer");
$this->expectExceptionMessage("Failed to write to stream after multiple attempts");

// The first write still succeeds somehow...
wait($stream->write("foobar"));
Expand Down Expand Up @@ -88,7 +88,7 @@ public function testClosedRemoteSocketWithFork()
\fclose($b);

$this->expectException(StreamException::class);
$this->expectExceptionMessage("The stream was closed by the peer");
$this->expectExceptionMessage("Failed to write to stream after multiple attempts");

try {
// The first write still succeeds somehow...
Expand Down

0 comments on commit b55e8bd

Please sign in to comment.