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

Skip to content

Commit da73125

Browse files
committed
[Process] Use stream based storage to avoid memory issues
1 parent b702448 commit da73125

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ public function getOutput()
378378

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

381-
return $this->stdout;
381+
if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
382+
return '';
383+
}
384+
385+
return $ret;
382386
}
383387

384388
/**
@@ -395,16 +399,13 @@ public function getIncrementalOutput()
395399
{
396400
$this->requireProcessIsStarted(__FUNCTION__);
397401

398-
$data = $this->getOutput();
399-
400-
$latest = substr($data, $this->incrementalOutputOffset);
402+
$latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
403+
$this->incrementalOutputOffset = ftell($this->stdout);
401404

402405
if (false === $latest) {
403406
return '';
404407
}
405408

406-
$this->incrementalOutputOffset = strlen($data);
407-
408409
return $latest;
409410
}
410411

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

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

424-
return $this->stderr;
425+
if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
426+
return '';
427+
}
428+
429+
return $ret;
425430
}
426431

427432
/**
@@ -439,16 +444,13 @@ public function getIncrementalErrorOutput()
439444
{
440445
$this->requireProcessIsStarted(__FUNCTION__);
441446

442-
$data = $this->getErrorOutput();
443-
444-
$latest = substr($data, $this->incrementalErrorOutputOffset);
447+
$latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
448+
$this->incrementalErrorOutputOffset = ftell($this->stderr);
445449

446450
if (false === $latest) {
447451
return '';
448452
}
449453

450-
$this->incrementalErrorOutputOffset = strlen($data);
451-
452454
return $latest;
453455
}
454456

@@ -666,21 +668,29 @@ public function stop($timeout = 10, $signal = null)
666668
/**
667669
* Adds a line to the STDOUT stream.
668670
*
671+
* @internal
672+
*
669673
* @param string $line The line to append
670674
*/
671675
public function addOutput($line)
672676
{
673-
$this->stdout .= $line;
677+
fseek($this->stdout, 0, SEEK_END);
678+
fwrite($this->stdout, $line);
679+
fseek($this->stdout, $this->incrementalOutputOffset);
674680
}
675681

676682
/**
677683
* Adds a line to the STDERR stream.
678684
*
685+
* @internal
686+
*
679687
* @param string $line The line to append
680688
*/
681689
public function addErrorOutput($line)
682690
{
683-
$this->stderr .= $line;
691+
fseek($this->stderr, 0, SEEK_END);
692+
fwrite($this->stderr, $line);
693+
fseek($this->stderr, $this->incrementalErrorOutputOffset);
684694
}
685695

686696
/**
@@ -1126,8 +1136,8 @@ private function resetProcessData()
11261136
$this->exitcode = null;
11271137
$this->fallbackStatus = array();
11281138
$this->processInformation = null;
1129-
$this->stdout = null;
1130-
$this->stderr = null;
1139+
$this->stdout = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
1140+
$this->stderr = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
11311141
$this->process = null;
11321142
$this->latestSignal = null;
11331143
$this->status = self::STATUS_READY;

0 commit comments

Comments
 (0)
0