Skip to content

bug: libobs/util: pipe-posix.c pipes are made close-on-exec non-atomically, so concurrent spawns inherit them #266

Closed

Description

@b7216309-jpg

os_process_pipe_create_internal (libobs/util/pipe-posix.c) creates its two pipes with pipe() and only then sets FD_CLOEXEC with fcntl(). If another thread spawns a process in that window, the new child inherits all four pipe ends, without close-on-exec, for its whole lifetime.

Observed on Linux (#192): 10 ms into a write that should have failed, a /proc scan found an unrelated thread's sh -c and its sleep 0.05 child holding both ends of a dead child's stdin pipe, with fd flags 00/01. In OBS, two threads that each start a helper process at the same time (for example two outputs starting ffmpeg-mux) can hand pipe ends into each other's children. That keeps pipes open after their owner closed them, so EOF/EPIPE is delayed or never comes while the other child lives.

Measured with a standalone driver around the real pipe-posix.c (16 threads spawning sleep 0.05; Ubuntu 26.04, WSL2):

writes that should fail but don't
pipe() + fcntl (master) 2–6 per 3000
pipe2(..., O_CLOEXEC) 1 per 9000

The remaining case is the expected transient one: a child briefly holds copies of every fd until its exec. Its inflated count is #264.

Fix options

  1. Linux and FreeBSD: pipe2(fds, O_CLOEXEC) instead of pipe() + fcntl. That's atomic.
  2. macOS has no pipe2. posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT) makes each child close every fd that isn't named in its file actions. That protects in the other direction and also covers fds from other libraries, but it changes what OBS's children inherit, so it's a separate decision. Without it, macOS keeps pipe() + fcntl and the window.
  3. Stopgap everywhere: serialize spawns in libobs with a lock held from pipe() to fcntl. That only covers spawns that go through libobs, not other libraries' fork/exec.

Testing question

The window is a few instructions wide, so a test is necessarily a stress test, like dead_pipe_write_survives_concurrent_spawns without its lock: many iterations of create-then-scan-children-fds, Linux-only. It would fail often but not every time before the fix. Is a probabilistic RED acceptable here? The alternative is a deterministic check of the implementation (for example -Wl,--wrap=pipe asserting os_process_pipe_create uses pipe2 with O_CLOEXEC), which tests how the fix is written rather than what it prevents.

I'll open the PR once the test approach is agreed; option 1 is ready otherwise.

Activity

  1. zackees commented on Oct 11, 2026

    @zackees
    Member

    Thanks, the /proc evidence is convincing for the inheritance path. One data point that disagrees, so the fix gets checked against both:

    On branch repro/192-pipe-parity-race (process::dead_pipe_write_survives_concurrent_spawns in rust/obs-util/tests/pipe_parity.rs, which drives the in-tree C oracle: 16 threads spawning sleep 0.05 and 3000 dead-pipe writes), I measured:

    • master pipe() + fcntl: 11 and 9 leaks per 3000.
    • pipe2(fds, O_CLOEXEC) (Linux): 38, 45 and 46 per 3000, not fewer.
    • Every leaked write returned the full 1 MiB. That can only happen if some process is reading the pipe, and a sleep holding an inherited fd never reads.

    So either my pipe2 run was invalid (a stale oracle object is possible: build.rs watches libobs, but I didn't confirm the rebuild with a probe), or a second mechanism exists alongside inheritance. Before landing option 1, could you run that branch's test with your pipe2 change? If it goes to roughly 0 per 3000, my run was stale and option 1 is confirmed. If it doesn't, the full-1 MiB writes point somewhere else.

    (#192 is the flaky test this explains. Whichever fix lands should take that test from RED to GREEN.)

  2. zackees commented on Oct 23, 2026

    @zackees
    Member

    Resolved by the port: util/pipe-posix.c.h is in the ported-posix group, so the hybrid libobs no longer builds it. The Rust os_process_pipe_create (rust/obs-util/src/platform/unix/pipe.rs) spawns through std::process::Command, whose pipes are created with pipe2(O_CLOEXEC) atomically, so concurrent spawns cannot inherit them. The non-atomic fcntl path now exists only in the C reference/oracle, which keeps upstream behaviour by policy.

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