Skip to content

CLI: output is held until the script exits (flush()/fflush() have no effect) #1

Description

@NikitaSomik

phpr prints nothing until the script ends: everything written with echo,
fwrite(STDOUT), php://stdout or php://output is kept in memory and
written at exit, and flush() / fflush(STDOUT) don't change that. If the
process is killed, the output is lost. Writing to /dev/stdout or STDERR
comes out immediately.

This breaks interactive CLI programs (TUI games, prompts), progress output
and long-running scripts that stream logs.

Reproduce

<?php
echo "tick 1\n";
flush();
sleep(2);
echo "tick 2\n";
phpr flush.php | while IFS= read -r l; do echo "$l at +${SECONDS}s"; done
  • PHP 8.5.7: tick 1 appears immediately, tick 2 two seconds later.
  • phpr: both lines appear together after two seconds.

The cause seems to be crates/php-cli/src/main.rs, which writes
outcome.rendered to stdout only after run_source_with_argv returns.

Found while running a terminal Snake game (plain PHP 8.4+) on phpr. The game
itself passes its PHPUnit suite and produces identical benchmark checksums on
phpr; only the interactive mode is affected. A local workaround (an output
buffer flushed to fopen('/dev/stdout')) makes it playable.

Environment: macOS 26.6.2 (Apple M1 Pro), Rust 1.96.0, commit 352d4129.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions