bug #19118 [Process] Fix pipes cleaning on Windows (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Process] Fix pipes cleaning on Windows

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19089
| License       | MIT
| Doc PR        | -

Commits
-------

d54cd02 [Process] Fix pipes cleaning on Windows
This commit is contained in:
Romain Neutron 2016-06-21 15:46:37 +02:00
commit ec19a527be
2 changed files with 26 additions and 6 deletions

View File

@ -47,14 +47,31 @@ class WindowsPipes extends AbstractPipes
// Workaround for this problem is to use temporary files instead of pipes on Windows platform.
//
// @see https://bugs.php.net/bug.php?id=51800
$this->files = array(
Process::STDOUT => tempnam(sys_get_temp_dir(), 'out_sf_proc'),
Process::STDERR => tempnam(sys_get_temp_dir(), 'err_sf_proc'),
$pipes = array(
Process::STDOUT => Process::OUT,
Process::STDERR => Process::ERR,
);
foreach ($this->files as $offset => $file) {
if (false === $file || false === $this->fileHandles[$offset] = @fopen($file, 'rb')) {
throw new RuntimeException('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable');
$tmpDir = sys_get_temp_dir();
if (!@fopen($file = $tmpDir.'\\sf_proc_00.check', 'wb')) {
throw new RuntimeException('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable');
}
@unlink($file);
for ($i = 0;; ++$i) {
foreach ($pipes as $pipe => $name) {
$file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
if (file_exists($file) && !@unlink($file)) {
continue 2;
}
$h = @fopen($file, 'xb');
if (!$h || !$this->fileHandles[$pipe] = fopen($file, 'rb')) {
continue 2;
}
if (isset($this->files[$pipe])) {
@unlink($this->files[$pipe]);
}
$this->files[$pipe] = $file;
}
break;
}
}

View File

@ -54,6 +54,9 @@ class ProcessTest extends \PHPUnit_Framework_TestCase
public function testThatProcessDoesNotThrowWarningDuringRun()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test is transient on Windows');
}
@trigger_error('Test Error', E_USER_NOTICE);
$process = $this->getProcess(self::$phpBin." -r 'sleep(3)'");
$process->run();