Skip to content

[2.7][Process] Fix memory issue when using large input streams #18015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/Symfony/Component/Process/Pipes/UnixPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
if (is_resource($input)) {
$this->input = $input;
} else {
$this->inputBuffer = (string) $input;
$this->input = fopen('php://temp', 'w+');
fwrite($this->input, $input);
fseek($this->input, 0);
}
}

Expand Down Expand Up @@ -147,16 +149,16 @@ public function readAndWrite($blocking, $close = false)
// lose key association, we have to find back the key
$type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input';
$data = '';
while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
$data .= $dataread;
}

if ('' !== $data) {
if ($type === 'input') {
$this->inputBuffer .= $data;
} else {
$read[$type] = $data;
if ($type !== 'input') {
while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
$data .= $dataread;
}
// Remove extra null chars returned by fread
if ('' !== $data) {
$read[$type] = rtrim($data, "\x00");
}
} elseif (isset($w[0])) {
stream_copy_to_stream($this->input, $w[0], 4096);
}

if (false === $data || (true === $close && feof($pipe) && '' === $data)) {
Expand All @@ -171,19 +173,8 @@ public function readAndWrite($blocking, $close = false)
}
}

if (null !== $w && 0 < count($w)) {
while (strlen($this->inputBuffer)) {
$written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k
if ($written > 0) {
$this->inputBuffer = (string) substr($this->inputBuffer, $written);
} else {
break;
}
}
}

// no input to read on resource, buffer is empty and stdin still open
if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
// no input to read on resource and stdin still open
if (null === $this->input && isset($this->pipes[0])) {
fclose($this->pipes[0]);
unset($this->pipes[0]);
}
Expand Down