Skip to content

libobs/util: Stop counting discarded data in os_process_pipe_write - #265

Merged
zackees merged 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/pipe-write-accounting
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
b7216309-jpg:fix/pipe-write-accounting

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Fixes #264. Root cause from the #192 investigation; details and the syscall trace are on #192.

Bug

os_process_pipe_write (pipe-posix.c) retried fwrite until it returned 0. A short fwrite already reports an error, and after one, glibc's fwrite copies up to 4096 bytes into the stdio buffer, fails the flush with EPIPE, discards them and still returns the copied count. So a write to a child that has exited reported the full length: the kernel took at most one pipe buffer (64 KiB) before EPIPE, and nothing ever read it.

Impact: the frontend blocks SIGPIPE, and obs-ffmpeg-mux.c writes every recorded packet to ffmpeg-mux with this function, failing only on ret != size. If ffmpeg-mux dies mid-recording, every later write "succeeds", so OBS keeps recording and discards everything, with no failure signalled until stop.

Commits

  1. Test (test/cmocka/test_pipe.c, Linux): a dd bs=1 count=1 of=/dev/null child (stderr closed first, so draining it can't block) reads one byte of a 16 MiB write and exits.
    • The write must report 0 < written < len, and the next write must report 0.
    • SIGPIPE is ignored for the test and restored before the assertions.
    • No sleeps and no concurrency: dd can't exit before the first byte arrives, so the short count is deterministic.
  2. Fix: return the result of one fwrite: the full length on success, the accepted prefix on a write error, and 0 once ferror is set. The Rust core's doc comment on Pipe::write drops the mention of the old loop. Its behavior already matched: it writes unbuffered and returns real byte counts.

One behavior to note: after any write error the stream stays in its error state, so later writes return 0. That's what callers want for EPIPE. A write interrupted by a signal could also set it, but pipe writes with SIGPIPE blocked rarely see EINTR, and the old loop didn't recover meaningfully either.

Testing

On Ubuntu 26.04 (WSL2), gcc 15, real cmocka 2.0, with test_pipe.c built against pipe.c, dstr.c, dstr-libc.c and the two versions of pipe-posix.c:

  • master: the new test fails 20/20 runs (written < len is not true); the 14 other tests pass.
  • fix: all 15 tests pass, 20/20 runs.
  • Rust: cargo test -p obs-util --test pipe --test pipe_parity passes 3/3 with the fixed C as the oracle.

Windows (pipe-windows.c) does one WriteFile per call and isn't affected.

The root cause, the standalone reproduction and the trace come from a separate investigation that I ran; I re-verified the fix and the test on current master as above.

🤖 Generated with Claude Code

b7216309-jpg and others added 2 commits October 11, 2026 22:45
On Linux, a dd child reads one byte of a 16 MiB write and exits. The
write must report a positive short count (the prefix the kernel took
before EPIPE), and the next write must report 0.

os_process_pipe_write retries fwrite until it returns 0, and after an
error glibc's fwrite copies 4096 bytes into its buffer, fails the flush,
discards them and still returns 4096. So the function reports the full
16 MiB as written, and the test fails until the next commit (20 of 20
runs: "written < len is not true").

Refs 264

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A short fwrite already reports an error. The loop retried it until
fwrite returned 0; after the first failure, each retry copied up to
4096 bytes into the stdio buffer, failed to flush and discarded them,
yet returned the copied count. A write to a child that had exited so
reported the full length, and obs-ffmpeg-mux, which only checks
ret != size, kept sending packets to a dead ffmpeg-mux with no error.

os_process_pipe_write now returns the result of one fwrite: the full
length on success, the accepted prefix on a write error, and 0 once the
stream has an error.

Fixes 264

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b7216309-jpg
b7216309-jpg requested a review from zackees as a code owner October 11, 2026 20:46
@zackees
zackees merged commit f692bf1 into obs-rust:master Oct 11, 2026
21 of 23 checks passed
zackees pushed a commit that referenced this pull request Oct 11, 2026
#265 merged with one line of test_pipe_write_stops_after_partial_error
not clang-formatted, so the clang-format check (and the local-gate lint
lane in Rust Tests) fails on master. Formatting only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs/util: os_process_pipe_write reports discarded data as written after EPIPE (POSIX)

2 participants