Skip to content

[Process] Use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr #59949

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
Mar 13, 2025

Conversation

joelwurtz
Copy link
Contributor

@joelwurtz joelwurtz commented Mar 10, 2025

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #59927
License MIT

This PR split the stderr in a pipe to correctly split stdout / stderr when using ->setPty

When using pty for pipes, PHP will only open only one pty and share the "same" fd for all pipes (it use dup but the original fd is the same)

Which means there is a possibility than stdout goes to stderr (or vice versa) when using pty for both stdout and stderr.

I did not make a test as this behavior is erratic and happens under other conditions that are hard to find (see related issue to see reproducer, we have to do some iterations before it happens)

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

Can you please add a test case or is it hard to achieve?

Was written in den PR header

@OskarStark OskarStark changed the title fix(process): use a pipe for stderr in pty mode to avoid mixed output… fix(process): use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr Mar 10, 2025
@joelwurtz
Copy link
Contributor Author

Like i said, test is hard to achieve it happens randomly our reproducer for this is the current script :

<?php

require 'vendor/autoload.php';

use Symfony\Component\Process\Process;

$reference = run_process();

echo "Output Reference:\n";
echo $reference;
echo "\n";

for ($i=0; $i < 100_000; $i++) {
    if ($i > 0 && $i % 80 === 0) {
        echo "\n";
    }
    echo '.';
    $output = run_process();
    if ($output !== $reference) {
        echo "\n!!Output changed!!\n";
        echo "New output:\n";
        echo "$output\n";
        exit(1);
    }
}

function run_process(){
    $process = new Process(['cat', __FILE__]);
    // If you comment the following line, the test become stable
    $process->setPty(true);
    $process->mustRun();

    return $process->getOutput();
}

@joelwurtz joelwurtz force-pushed the fix/fix-mixed-stdout-stderr branch 2 times, most recently from f6b1b56 to 001b418 Compare March 10, 2025 16:57
@joelwurtz
Copy link
Contributor Author

I spoke to soon, i added a test, before this change everything was in the getOutput

@joelwurtz
Copy link
Contributor Author

Failed test seems to not be related, so ready for review i guess

@carsonbot carsonbot changed the title fix(process): use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr [Process] fix(process): use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr Mar 13, 2025
@nicolas-grekas nicolas-grekas changed the title [Process] fix(process): use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr [Process] Use a pipe for stderr in pty mode to avoid mixed output between stdout and stderr Mar 13, 2025
@nicolas-grekas
Copy link
Member

Good catch, thanks @joelwurtz.

@nicolas-grekas nicolas-grekas merged commit bb07d18 into symfony:6.4 Mar 13, 2025
10 of 11 checks passed
@joelwurtz joelwurtz deleted the fix/fix-mixed-stdout-stderr branch March 21, 2025 09:23
This was referenced Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants