Skip to content

Commit

Permalink
Fix ResourceOutputStream closed detection (#60)
Browse files Browse the repository at this point in the history
The previous mechanism with feof doesn't work for systemd launched processes.

Fixes #58.
  • Loading branch information
kelunik committed Jun 3, 2019
1 parent 24484fa commit c75af37
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct($stream, int $chunkSize = null)
continue;
}

if (!\is_resource($stream) || @\feof($stream)) {
if (!\is_resource($stream) || (($metaData = @\stream_get_meta_data($stream)) && $metaData['eof'])) {
throw new StreamException("The stream was closed by the peer");
}

Expand Down Expand Up @@ -179,8 +179,8 @@ 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"));
if (!\is_resource($this->resource) || (($metaData = @\stream_get_meta_data($this->resource)) && $metaData['eof'])) {
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.
Expand Down
4 changes: 2 additions & 2 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; fwrite(): send of 6 bytes failed with errno=32 Broken pipe");
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; fwrite(): send of 6 bytes failed with errno=32 Broken pipe");

// The first write still succeeds somehow...
wait($stream->write("foobar"));
Expand Down

0 comments on commit c75af37

Please sign in to comment.