Skip to content

Commit

Permalink
Use \fclose for unidirectional resources to cover pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jun 18, 2017
1 parent 9354089 commit 798723e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ final class ResourceOutputStream implements OutputStream {
/** @var int|null */
private $chunkSize;

/** @var bool Flag to avoid \fclose() inside destructor */
private $inDestructor = false;

/**
* @param resource $stream Stream resource.
* @param int|null $chunkSize Chunk size per `fwrite()` operation.
Expand Down Expand Up @@ -203,8 +206,14 @@ private function send(string $data, bool $end = false): Promise {
* @return void
*/
public function close() {
if ($this->resource) {
\stream_socket_shutdown($this->resource, \STREAM_SHUT_WR);
if ($this->resource && !$this->inDestructor) {
$meta = \stream_get_meta_data($this->resource);

if (strpos($meta["mode"], "+") !== false) {
\stream_socket_shutdown($this->resource, \STREAM_SHUT_WR);
} else {
\fclose($this->resource);
}
}

$this->resource = null;
Expand All @@ -230,6 +239,8 @@ public function getResource() {
}

public function __destruct() {
$this->inDestructor = true;

if ($this->resource !== null) {
$this->close();
}
Expand Down

0 comments on commit 798723e

Please sign in to comment.