Skip to content
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

[2.3][Process] Use stream based storage to avoid memory issues #17423

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -378,7 +378,11 @@ public function getOutput()

$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);

return $this->stdout;
if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
return '';
}

return $ret;
}

/**
Expand All @@ -395,16 +399,13 @@ public function getIncrementalOutput()
{
$this->requireProcessIsStarted(__FUNCTION__);

$data = $this->getOutput();

$latest = substr($data, $this->incrementalOutputOffset);
$latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
$this->incrementalOutputOffset = ftell($this->stdout);

if (false === $latest) {
return '';
}

$this->incrementalOutputOffset = strlen($data);

return $latest;
}

Expand All @@ -421,7 +422,11 @@ public function getErrorOutput()

$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);

return $this->stderr;
if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
return '';
}

return $ret;
}

/**
Expand All @@ -439,16 +444,13 @@ public function getIncrementalErrorOutput()
{
$this->requireProcessIsStarted(__FUNCTION__);

$data = $this->getErrorOutput();

$latest = substr($data, $this->incrementalErrorOutputOffset);
$latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
$this->incrementalErrorOutputOffset = ftell($this->stderr);

if (false === $latest) {
return '';
}

$this->incrementalErrorOutputOffset = strlen($data);

return $latest;
}

Expand Down Expand Up @@ -666,21 +668,29 @@ public function stop($timeout = 10, $signal = null)
/**
* Adds a line to the STDOUT stream.
*
* @internal
*
* @param string $line The line to append
*/
public function addOutput($line)
{
$this->stdout .= $line;
fseek($this->stdout, 0, SEEK_END);
fwrite($this->stdout, $line);
fseek($this->stdout, $this->incrementalOutputOffset);
}

/**
* Adds a line to the STDERR stream.
*
* @internal
*
* @param string $line The line to append
*/
public function addErrorOutput($line)
{
$this->stderr .= $line;
fseek($this->stderr, 0, SEEK_END);
fwrite($this->stderr, $line);
fseek($this->stderr, $this->incrementalErrorOutputOffset);
}

/**
Expand Down Expand Up @@ -1126,8 +1136,8 @@ private function resetProcessData()
$this->exitcode = null;
$this->fallbackStatus = array();
$this->processInformation = null;
$this->stdout = null;
$this->stderr = null;
$this->stdout = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
$this->stderr = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
$this->process = null;
$this->latestSignal = null;
$this->status = self::STATUS_READY;
Expand Down