Skip to content

Commit 8d7f6ed

Browse files
committed
bug #18015 [2.7][Process] Fix memory issue when using large input streams (romainneutron)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7][Process] Fix memory issue when using large input streams | Q | A | ------------- | --- | Branch | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17667 | License | MIT This removes buffering of a stream input. Tests shows that this implementation is much faster and fixes some issues when streams are based on very large inputs Commits ------- fb8da9f [Process] Fix memory issue when using large input streams
2 parents e187f8b + fb8da9f commit 8d7f6ed

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/Symfony/Component/Process/Pipes/UnixPipes.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
3838
if (is_resource($input)) {
3939
$this->input = $input;
4040
} else {
41-
$this->inputBuffer = (string) $input;
41+
$this->input = fopen('php://temp', 'w+');
42+
fwrite($this->input, $input);
43+
fseek($this->input, 0);
4244
}
4345
}
4446

@@ -147,16 +149,16 @@ public function readAndWrite($blocking, $close = false)
147149
// lose key association, we have to find back the key
148150
$type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input';
149151
$data = '';
150-
while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
151-
$data .= $dataread;
152-
}
153-
154-
if ('' !== $data) {
155-
if ($type === 'input') {
156-
$this->inputBuffer .= $data;
157-
} else {
158-
$read[$type] = $data;
152+
if ($type !== 'input') {
153+
while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
154+
$data .= $dataread;
155+
}
156+
// Remove extra null chars returned by fread
157+
if ('' !== $data) {
158+
$read[$type] = rtrim($data, "\x00");
159159
}
160+
} elseif (isset($w[0])) {
161+
stream_copy_to_stream($this->input, $w[0], 4096);
160162
}
161163

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

174-
if (null !== $w && 0 < count($w)) {
175-
while (strlen($this->inputBuffer)) {
176-
$written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k
177-
if ($written > 0) {
178-
$this->inputBuffer = (string) substr($this->inputBuffer, $written);
179-
} else {
180-
break;
181-
}
182-
}
183-
}
184-
185-
// no input to read on resource, buffer is empty and stdin still open
186-
if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
176+
// no input to read on resource and stdin still open
177+
if (null === $this->input && isset($this->pipes[0])) {
187178
fclose($this->pipes[0]);
188179
unset($this->pipes[0]);
189180
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy