Skip to content

Commit 6d8078f

Browse files
committed
bug symfony#17423 [2.3][Process] Use stream based storage to avoid memory issues (romainneutron)
This PR was merged into the 2.3 branch. Discussion ---------- [2.3][Process] Use stream based storage to avoid memory issues | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#17390 | License | MIT Commits ------- da73125 [Process] Use stream based storage to avoid memory issues
2 parents 1941add + da73125 commit 6d8078f

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
@@ -375,7 +375,11 @@ public function getOutput()
375375

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

378-
return $this->stdout;
378+
if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
379+
return '';
380+
}
381+
382+
return $ret;
379383
}
380384

381385
/**
@@ -392,16 +396,13 @@ public function getIncrementalOutput()
392396
{
393397
$this->requireProcessIsStarted(__FUNCTION__);
394398

395-
$data = $this->getOutput();
396-
397-
$latest = substr($data, $this->incrementalOutputOffset);
399+
$latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
400+
$this->incrementalOutputOffset = ftell($this->stdout);
398401

399402
if (false === $latest) {
400403
return '';
401404
}
402405

403-
$this->incrementalOutputOffset = strlen($data);
404-
405406
return $latest;
406407
}
407408

@@ -418,7 +419,11 @@ public function getErrorOutput()
418419

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

421-
return $this->stderr;
422+
if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
423+
return '';
424+
}
425+
426+
return $ret;
422427
}
423428

424429
/**
@@ -436,16 +441,13 @@ public function getIncrementalErrorOutput()
436441
{
437442
$this->requireProcessIsStarted(__FUNCTION__);
438443

439-
$data = $this->getErrorOutput();
440-
441-
$latest = substr($data, $this->incrementalErrorOutputOffset);
444+
$latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
445+
$this->incrementalErrorOutputOffset = ftell($this->stderr);
442446

443447
if (false === $latest) {
444448
return '';
445449
}
446450

447-
$this->incrementalErrorOutputOffset = strlen($data);
448-
449451
return $latest;
450452
}
451453

@@ -663,21 +665,29 @@ public function stop($timeout = 10, $signal = null)
663665
/**
664666
* Adds a line to the STDOUT stream.
665667
*
668+
* @internal
669+
*
666670
* @param string $line The line to append
667671
*/
668672
public function addOutput($line)
669673
{
670-
$this->stdout .= $line;
674+
fseek($this->stdout, 0, SEEK_END);
675+
fwrite($this->stdout, $line);
676+
fseek($this->stdout, $this->incrementalOutputOffset);
671677
}
672678

673679
/**
674680
* Adds a line to the STDERR stream.
675681
*
682+
* @internal
683+
*
676684
* @param string $line The line to append
677685
*/
678686
public function addErrorOutput($line)
679687
{
680-
$this->stderr .= $line;
688+
fseek($this->stderr, 0, SEEK_END);
689+
fwrite($this->stderr, $line);
690+
fseek($this->stderr, $this->incrementalErrorOutputOffset);
681691
}
682692

683693
/**
@@ -1123,8 +1133,8 @@ private function resetProcessData()
11231133
$this->exitcode = null;
11241134
$this->fallbackStatus = array();
11251135
$this->processInformation = null;
1126-
$this->stdout = null;
1127-
$this->stderr = null;
1136+
$this->stdout = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
1137+
$this->stderr = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
11281138
$this->process = null;
11291139
$this->latestSignal = null;
11301140
$this->status = self::STATUS_READY;

0 commit comments

Comments
 (0)