Skip to content

[Process] Fix pipes cleaning on Windows #19118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/Symfony/Component/Process/Pipes/WindowsPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,31 @@ public function __construct($disableOutput, $input)
// 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')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if another process is started at the same time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wb allows it without any issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then

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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function tearDown()

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();
Expand Down