8000 [Process] Use stream based storage to avoid memory issues · symfony/symfony@d53a370 · GitHub
[go: up one dir, main page]

Skip to content

Commit d53a370

Browse files
committed
[Process] Use stream based storage to avoid memory issues
1 parent 31aef7b commit d53a370

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public function getOutput()
474474

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

477-
return $this->stdout;
477+
return (string) stream_get_contents($this->stdout, -1, 0);
478478
}
479479

480480
/**
@@ -490,17 +490,14 @@ public function getOutput()
490490
*/
491491
public function getIncrementalOutput()
492492
{
493-
$this->requireProcessIsStarted(__FUNCTION__);
494-
495-
$data = $this->getOutput();
496-
497-
$latest = substr($data, $this->incrementalOutputOffset);
498-
499-
if (false === $latest) {
500-
return '';
493+
if ($this->outputDisabled) {
494+
throw new LogicException('Output has been disabled.');
501495
}
502496

503-
$this->incrementalOutputOffset = strlen($data);
497+
$this->requireProcessIsStarted(__FUNCTION__);
498+
499+
$latest = (string) stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
500+
$this->incrementalOutputOffset = ftell($this->stdout);
504501

505502
return $latest;
506503
}
@@ -512,7 +509,8 @@ public function getIncrementalOutput()
512509
*/
513510
public function clearOutput()
514511
{
515-
$this->stdout = '';
512+
ftruncate($this->stdout, 0);
513+
fseek($this->stdout, 0);
516514
$this->incrementalOutputOffset = 0;
517515

518516
return $this;
@@ -536,7 +534,7 @@ public function getErrorOutput()
536534

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

539-
return $this->stderr;
537+
return (string) stream_get_contents($this->stderr, -1, 0);
540538
}
541539

542540
/**
@@ -553,17 +551,14 @@ public function getErrorOutput()
553551
*/
554552
public function getIncrementalErrorOutput()
555553
{
556-
$this->requireProcessIsStarted(__FUNCTION__);
557-
558-
$data = $this->getErrorOutput();
559-
560-
$latest = substr($data, $this->incrementalErrorOutputOffset);
561-
562-
if (false === $latest) {
563-
return '';
554+
if ($this->outputDisabled) {
555+
throw new LogicException('Output has been disabled.');
564556
}
565557

566-
$this->incrementalErrorOutputOffset = strlen($data);
558+
$this->requireProcessIsStarted(__FUNCTION__);
559+
560+
$latest = (string) stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
561+
$this->incrementalErrorOutputOffset = ftell($this->stderr);
567562

568563
return $latest;
569564
}
@@ -575,7 +570,8 @@ public function getIncrementalErrorOutput()
575570
*/
576571
public function clearErrorOutput()
577572
{
578-
$this->stderr = '';
573+
ftruncate($this->stderr, 0);
574+
fseek($this->stderr, 0);
579575
$this->incrementalErrorOutputOffset = 0;
580576

581577
return $this;
@@ -795,23 +791,31 @@ public function stop($timeout = 10, $signal = null)
795791
/**
796792
* Adds a line to the STDOUT stream.
797793
*
794+
* @internal
795+
*
798796
* @param string $line The line to append
799797
*/
800798
public function addOutput($line)
801799
{
802800
$this->lastOutputTime = microtime(true);
803-
$this->stdout .= $line;
801+
fseek($this->stdout, 0, SEEK_END);
802+
fwrite($this->stdout, $line);
803+
fseek($this->stdout, $this->incrementalOutputOffset);
804804
}
805805

806806
/**
807807
* Adds a line to the STDERR stream.
808808
*
809+
* @internal
810+
*
809811
* @param string $line The line to append
810812
*/
811813
public function addErrorOutput($line)
812814
{
813815
$this->lastOutputTime = microtime(true);
814-
$this->stderr .= $line;
816+
fseek($this->stderr, 0, SEEK_END);
817+
fwrite($this->stderr, $line);
818+
fseek($this->stderr, $this->incrementalErrorOutputOffset);
815819
}
816820

817821
/**
@@ -1393,8 +1397,8 @@ private function resetProcessData()
13931397
$this->exitcode = null;
13941398
$this->fallbackStatus = array();
13951399
$this->processInformation = null;
1396-
$this->stdout = null;
1397-
$this->stderr = null;
1400+
$this->stdout = fopen('php://temp/maxmemory:'.(8 * 1024 * 1024), 'a+');
1401+
$this->stderr = fopen('php://temp/maxmemory:'.(8 * 1024 * 1024), 'a+');
13981402
$this->process = null;
13991403
$this->latestSignal = null;
14001404
$this->status = self::STATUS_READY;

0 commit comments

Comments
 (0)
0