Skip to content

Flaky: pipe_parity dead_pipe_write_matches_oracle (oracle write returns full length) #192

Closed

Description

@zackees

Seen in CI on #188 (job 114380330215, ubuntu-26.04, Rust Tests). #188 doesn't touch Rust.

panicked at rust/obs-util/tests/pipe_parity.rs:422:9:
assertion `left == right` failed
  left: 0        (shim)
 right: 1048576  (oracle_os_process_pipe_write)

The C oracle wrote all 1 MiB into a pipe whose child had already closed stdin and exited, so another process still held the read end. The likely cause is a file-descriptor inheritance race between parallel tests. A child spawned concurrently by another test in the same binary inherits this pipe's fd (the oracle's pipe-posix path does not set CLOEXEC on every fd before the fork). Fixing it means one of two things: run the process tests serially (a mutex shared by every spawning test), or confirm and fix the missing O_CLOEXEC in the oracle or Rust spawn path. Before fixing, add a RED test that reproduces it by spawning concurrently.

Activity

  1. zackees commented on Oct 11, 2026

    @zackees
    MemberAuthor

    Root cause: the race is in the C oracle, not the Rust port.

    libobs/util/pipe-posix.c creates its pipes with plain pipe() (:51, :55) and only marks them FD_CLOEXEC later, with fcntl (:71-74). cargo test runs the pipe_parity tests on parallel threads. If another test's posix_spawn or Command::spawn runs inside that window, its child inherits this test's pipe fds without close-on-exec and keeps the pipe alive. The oracle's write to a "dead" child then succeeds (right: 1048576). Rust's Command creates fds with O_CLOEXEC atomically, so the shim side can't race this way.

    Fix plan, RED then GREEN:

    1. RED: a stress test that runs N threads spawning through oracle_os_process_pipe_create while one thread repeats dead_pipe_write_matches_oracle's scenario, and asserts that the oracle write reports a short count every time. It should fail reliably within a few hundred iterations.
    2. GREEN: serialize every test in pipe_parity.rs that spawns, behind one static SPAWN: Mutex<()>. The oracle has to stay byte-for-byte the original C.

    Separately, the C race is a real libobs bug (any multithreaded OBS plugin that spawns can leak these fds). The C fix is pipe2(..., O_CLOEXEC) where available. That's an upstream-behaviour change, so it would be its own issue and PR, and the Rust port already behaves that way.

  2. zackees commented on Oct 11, 2026

    @zackees
    MemberAuthor

    Update: I have a reliable reproducer, and my earlier root cause was wrong.

    Branch repro/192-pipe-parity-race (90dc713be) adds process::dead_pipe_write_survives_concurrent_spawns to pipe_parity.rs. 16 threads spawn and destroy sleep 0.05 read pipes through the C oracle while the main thread does 3000 iterations of the dead-pipe scenario (child prints to stderr, closes stdin and exits; parent drains stderr to EOF and then writes 1 MiB). Result: 11 and 9 of 3000 writes succeeded in two runs (about 18 s each).

    What I ruled out:

    • Not the pipe()/fcntl(FD_CLOEXEC) window. Switching pipe-posix.c to pipe2(O_CLOEXEC) still leaked 38, 45 and 46 of 3000.
    • The leaked writes are complete: wrote = 1048576 every time. A 1 MiB write only completes if some process is reading the pipe, so this isn't a stray fd held by a sleep (which never reads stdin).
    • I found no double close in os_process_pipe_destroy or in the post-spawn path.

    Next suspects: the dead child's /bin/sh -c is not the process that ends up reading, so something is reading on its behalf. Check whether stderr EOF can arrive before the stdin read end is really gone, and whether waitpid in a concurrent destroy can reap or confuse the wrong pid. Also run the same stress against the Rust shim alone, to see whether this is C-only.

    I'm leaving this unclaimed for anyone who wants it. The reproducer is a solid RED to start from.

  3. b7216309-jpg commented on Oct 11, 2026

    @b7216309-jpg
    Contributor

    I reproduced this in a local Ubuntu 26.04 VM (WSL2, kernel 6.18). Your dead_pipe_write_survives_concurrent_spawns gives 2 of 3000. I also have a standalone C driver around the real pipe-posix.c, with the same scenario and 16 sleep 0.05 spawner threads, which reports what each leaked write went to. It leaks 2–6 per 3000.

    The pipe() → fcntl(FD_CLOEXEC) window is the main cause. A watcher thread scanned /proc/*/fd 10 ms into a leaked write. It found a spawner's sh -c and its sleep 0.05 child holding both ends of the dead child's stdin pipe, at the same fd numbers as the test process, with fd flags 00/01 (no O_CLOEXEC). The test process's own copy has 02000001.

    LEAK #1: wrote 1048576 in 52.44 ms, write fd 17 -> pipe:[88776], child 32989 already exited
      during: pid 29775 fd 17 flags 02000001 state S: ./diag192 3000
      during: pid 32986 fd 16 flags 00 state S: sh -c
      during: pid 32986 fd 17 flags 01 state S: sh -c
      during: pid 33004 fd 16 flags 00 state S: sleep 0.05
      during: pid 33004 fd 17 flags 01 state S: sleep 0.05
    

    The leaked writes take about 52 ms, which is how long that sleep lives. In libobs the same race leaks one thread's pipe ends into another thread's child for that child's whole lifetime, for example when two outputs start ffmpeg at the same moment.

    pipe2(O_CLOEXEC) does help here. I changed only the two pipe() calls in a copy of pipe-posix.c and ran the same driver 3 × 3000: 1, 0 and 0 leaks, against about 4 per 3000 before. Your 38/45/46 run may have hit a stale C oracle build: soldr's cache has replayed stale oracle objects for me before.

    A rarer leak remains with pipe2, and I can't explain it yet. The child hadn't been reaped yet (waitpid(WNOHANG) = 0) and the full 1 MiB write took 0.27 ms. The pipe is 64 KiB (F_GETPIPE_SZ), so something read it. Either way, any concurrent spawn briefly holds copies of every fd until its exec, so the dead-pipe assertion can't be reliable while other threads spawn.

    So, as you planned:

    • Tests: serialize spawning in pipe_parity.rs. I'm opening that PR on top of your reproducer, with your commit and authorship kept.
    • C: pipe2(..., O_CLOEXEC) in pipe-posix.c where available, as its own issue and PR. The Rust port already gets atomic O_CLOEXEC from std.
  4. b7216309-jpg commented on Oct 11, 2026

    @b7216309-jpg
    Contributor

    Follow-up on the remaining case I couldn't explain (a full 1 MiB "written" in about 0.27 ms through a 64 KiB pipe). A separate investigation that I ran found the cause, and I re-checked it on current master. No process read the data: the count is false accounting in os_process_pipe_write.

    while (written < len) {
    	size_t ret = fwrite(data + written, 1, len - written, pp->file);
    	if (!ret)
    		return written;
    	written += ret;
    }

    Once a short-lived child (still before its exec) briefly holds the read end:

    • the kernel accepts 64 KiB, then the write gets EPIPE;
    • fwrite returns 65536 with ferror set, which already means "error" (glibc manual);
    • the loop only stops on 0, so it calls fwrite again;
    • each later call copies 4096 bytes into the stdio buffer, fails the flush with EPIPE, discards the buffer and still returns 4096.

    So 65536 + 239 × 4096 + 4096 = 1048576. A syscall trace of a standalone reproduction (no spawning: a thread holds the read end until FIONREAD reports data, then closes it, and never reads) shows one write() = 65536, then 240 write() = -1 EPIPE, and no read of the pipe.

    This matters beyond the test. The frontend blocks SIGPIPE, and obs-ffmpeg-mux.c sends every recorded packet to the ffmpeg-mux child through os_process_pipe_write, treating only ret != size as failure. If ffmpeg-mux dies mid-recording, every later write still "succeeds": OBS keeps recording while discarding the data, with no error until stop.

    Verified on master 17fc4fae8 (Ubuntu 26.04 / WSL2, gcc 15, real cmocka 2.0):

    • A deterministic regression test (a dd bs=1 count=1 child, 16 MiB written, which must give 0 < written < len, and then 0) fails 20/20 against master's pipe-posix.c.
    • With the fix (return one fwrite result, and 0 once the stream has an error), all 15 test_pipe cases pass 20/20.
    • pipe and pipe_parity pass with the fixed C as the oracle. The Rust port never had this bug: it writes unbuffered and returns real byte counts.

    I'm opening that as its own issue and PR. The pipe()/fcntl race from my earlier comment is a separate fix and gets its own issue.

  5. zackees commented on Oct 12, 2026

    @zackees
    MemberAuthor

    Another occurrence, this time on Windows: windows_process::dead_pipe_write_matches_oracle panicked at rust/obs-util/tests/pipe_parity.rs:874 in the Rust Tests (windows-2025-vs2026) job of #395 (run 38283726776, job 114927114390). #395 only touches libobs/obs-source-deinterlace.c, and master's runs right before and after passed. I reran the failed job.

  6. zackees commented on Oct 14, 2026

    @zackees
    MemberAuthor

    Still recurring on Windows: windows_process::dead_pipe_write_matches_oracle panicked at rust/obs-util/tests/pipe_parity.rs:874 in Rust Tests (windows-2025-vs2026) on #766 (run 38638167750, 2026-10-14). #766 only touches shared/properties-view (Qt) and a cmocka test, so it's the same flake as the #395 occurrence above. Reopening since the earlier fix didn't cover the Windows path; I re-ran the job.

  7. zackees commented on Oct 14, 2026

    @zackees
    MemberAuthor

    Claiming. Hypothesis to confirm first: on Windows the pipe handles are created inheritable, so a process spawned concurrently by another test thread can inherit a copy of the read end; then the 'dead pipe' write succeeds (full length) even though cmd exited.

  8. zackees commented on Oct 23, 2026

    @zackees
    MemberAuthor

    Fixed by #772 (merged 2026-10-14, "Fixes #192"), which didn't auto-close this issue. No recurrence reported since. Reopen if it shows up again.

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

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions