Skip to content

echo and fwrite(STDOUT, …) are held in a 4 KiB stdio buffer when stdout is a pipe: logs arrive late, after later stderr lines, and are lost on SIGTERM #544

Closed

Description

@lukas-kuna

What happened

When stdout is a pipe or a file, output from echo and fwrite(STDOUT, …) stays in libc's stdout buffer until 4 KiB have accumulated or the process exits normally. fwrite(STDERR, …) goes out at once. The PHP CLI writes both at once. In a long-running process this means:

  • a line-oriented log or a heartbeat line arrives in 4 KiB bursts;
  • when stdout and stderr share one pipe (2>&1, a process supervisor, systemd, Docker), stdout lines appear after stderr lines that were written later;
  • if the process is stopped with SIGTERM, the buffered lines are never written.

Steps to reproduce

<?php
// out.php
fwrite(STDOUT, "line 1 via fwrite(STDOUT)\n");
echo "line 2 via echo\n";
fwrite(STDERR, "line 3 via fwrite(STDERR)\n");
sleep(2);
fwrite(STDOUT, "line 4 after 2 s\n");

With a timestamp on every line read from the pipe (stamp = while IFS= read -r l; do echo "$(date +%S.%N | cut -c1-5) $l"; done):

$ manticore compile out.php -o out && ./out 2>&1 | stamp
13.49  line 3 via fwrite(STDERR)
15.49  line 1 via fwrite(STDOUT)
15.49  line 2 via echo
15.49  line 4 after 2 s
$ php -n out.php 2>&1 | stamp
15.50  line 1 via fwrite(STDOUT)
15.50  line 2 via echo
15.50  line 3 via fwrite(STDERR)
17.50  line 4 after 2 s

Stopped after one second (./out | cat > o.txt & sleep 1; kill <pid of out>): o.txt is empty with Manticore and has lines 1 and 2 with PHP.

The buffer size, with 100-byte lines written every 10 ms (echo or fwrite(STDOUT, …), 150 lines): the reader gets them in bursts of 40–41 lines (about 4 KiB) every 0.41 s. PHP delivers each line at once.

echo and fwrite(STDOUT, …) reach the same libc stdout (@__mir_out_write in EmitLlvmRuntime.php and fwrite() in src/Runtime/Stdlib/Io.php). echo flushes it only when implicit flush is on (@__mir_ob_implicit), which defaults to 0. The PHP CLI forces implicit_flush=1. With ob_implicit_flush(true) at the start of the program, echo lines arrive at once, but fwrite(STDOUT, …) still arrives in 4 KiB bursts.

How we hit it

A worker process whose supervisor reads its stdout line by line as log records and as a liveness heartbeat (one hb line every 250 ms). With Manticore no line arrived until we added fflush(STDOUT) after each write.

Expected

The same behaviour as the PHP CLI: implicit_flush on by default in the CLI, and fwrite(STDOUT, …) written out without waiting for a full buffer. Then output keeps its order relative to stderr and nothing is lost on SIGTERM.

Environment

Manticore 0.17.0 (release tarball manticore-0.17.0-linux-amd64) and main at 3b44bdea built with bin/build (same behaviour); Debian 13, Linux 6.12, x86-64; PHP 8.2.34 for comparison.

Activity

  1. added
    bug
    wrong-answer
    area:runtime
    on Nov 1, 2026
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

    area:runtimebugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions